In a previous previous post I gave mentioned the measuring of a tempreture, but I haven't shown how to do it there, so now I'm going to do that.
I'm using the cheap and easy-to-use thermometer DS18B20. It uses the One-Wire-Bus to communicate with other devices. The big advantage is that data of one or more sensor are sent over just one cable.
Besides the sensor I want to present a LC-Display, which is used with I2C. Normal HD44780-displays use a lot of pins. If you have bigger projects where every pin counts such displays come in handy.
"One-Wire" is a bit missleading, because you need at least a ground connection. Some sensors also need a power supply. But in most cases you can connect the power supply cable direct directly with the ground. Yes: Vcc to GND.
In my case I didn't to that, because why should I put the +5V cable to the ground, when there's the supply besides it? Ok, if you want to safe one more cable then it's good. Some One-Wire sensors can also be used on a 3.3V power supply. So you can use them on a Raspberry Pi for example.
For the following example you need some parts:
|1||Arduino||Uno or Nano|
|1||Resistor||4.7kΩ||For the data bus|
The sensors pinout:
To the one who are using the waterproof housing: You have to connect it depending on the cable's color:
To make everything clear, here's the shematic plan:
As you see, there's not much behind this setup. There are even sensors with integrated resistors, but they are more expensive.
By the way: 3€ for two sensors is a good deal!
The 16x2 display uses the commonly used HD44780 controller which needs many pins on your Arduino. Therefor it has a I2C backpack module on it, which translates the I2C commands into for it readable ones.
As said, the display uses the I2C bus. The pinout is the same as from the display I presented previously.
- SDA: A4
- SCL: A5
- Vcc: +5V
- GND: Ground
For those of you who doesn't want to use the display: The temperature is also sent to the serial monitor.
In the directories on GitHub are 5 folders. Dallas Temperature and OneWire are the used libraries for the temperature measuring. The directory named LiquidCrystal_I2C contains the library for the display. All three of them must be copied into the Arduinos standard directory for libraries (../Arduino/Libraries/). Is this done you can compile the sketch and upload it on the board.
Go to line 13. Here you'll find the definition of the OneWire bus pin. In this case it's pin 2.
#define ONEWIRE_BUS 2
To find out how many sensors are in use I use the function
This way I can switch between all connected sensors automatically. Until now I have not found a way where I can determine the index of each sensor. So I don't know which sensor has which ID when I connect them to the bus. The only way to find
out is: Heat up every sensor and check what temperature on which index changes.
To read the temperature you have to use:
oneWireSensors.requestTemperatures(); float Temp = oneWireSensors.getTempCByIndex(i);
The "Index" in the function getTempCByIndex refers to th sensor. Sensor 1 has index 0, sensor 2 has index 1, etc.
That's it. The rest of the code lines are here to update the display every two seconds without using a delay.
Maybe a last word to the Temperature.ino file. The bus is rather slow. The more sensors you use the slower it gets. So it actually needs up to one second when you want to read the data of two sensors.
This program is a small extension of the first one with a small scale. It only supports one sensor, but the temperature is shown on the scale in relation to it's limits.
You can also use the scale as a progress bar or something else.
void Scale (int Row, int Value, int Minimum_Value, int Maximum_Value)
Row is the line on the display, value is (of course) the value an min- and max-value are the limits.
E.g.: If you want the value of the sensor in line two in between it's usage range (-55°C to 125°C) you have to use the function this was:
Scale(1, int(Temperature), -55, 125);
Questions and comments: deloarts.wordpress.com