Signed in as:
filler@godaddy.com
Signed in as:
filler@godaddy.com
<--Back to Panther Logger Tutorials
In this tutorial we will read data from a DS18B20 temperature sensor using the Panther Logger.
Sections in this tutorial
To get started you will need the following:
1. Panther Logger board
2. DS18B20 temperature sensor (Digi Key Part# DS18B20+PAR-ND)
3. 4.7K resistor
Attach the DS18B20 sensor to block 6 screw terminal plug on the Panther Logger. To do this, hold the flat side of the DS18B20 sensor up and the sensor turned upside down (sensor wires up). See video to the right and picture below. In this position, the left most sensor wire is the power in (VIN) pin and aligns with the left most screw terminal port on the block 6 plug, which is one of the terminals for the switched 3.3V power rail (3VS). The two next to it are the other two we need for this sensor, the D6 digital pin for the sensor's signal wire and ground. Screw in the three wires from the sensor into these screw terminals for 3VS, D6 (signal), and ground. Next install a 4.7K resistor between 3VS pin and D6. When finished be sure the sensor wires are not bent and touching each other.
The DS18B20 temperature sensor correctly attached to the Panther Logger for this tutorial
The DS18B20 sensor communicates over the 1-wire protocol. Accordingly, in this tutorial we will use the OneWire library to read this sensor written by Paul Stoffregen. This library can be installed through the Arduino IDE. In the Arduino IDE menu go to Sketch>Include Library>Manage Libraries. In the search box that appears type OneWire. Find and click on the OneWire library from Paul Stoffregen. Select the latest version and click install. Close the dialog box.
The OneWire library comes with example scripts, but those will not work as is with the Panther Logger. Go to the DS18B20_Scan script in our Github library here. You can simply copy this code into a new file in Arduino. Click on the copy symbol in the upper right next to the "raw" button, then in the Arduino IDE go to file new. Erase everything in this new file and paste the code copied from Github. Save the new file with a name of your choice. Make sure the Panther Logger board is selected at Tools>Port and then upload the script to the Panther Logger. Open the serial monitor to view the data.
This script will scan for DS18B20 devices on the one wire bus by their address, read their temperature data and print the results to the serial monitor. If there is more than one DS18B20 device on the bus then it will go to the next address and report the data. If there is no more devices then it will report "no more addresses." Similarly, if no devices can be found then it will only print "no more addresses" to the serial monitor.
Touch the sensor and you should see the temperature as reported to the serial monitor begin to increase and then decrease when you remove your finger.
In this tutorial we have one device connected to the board, so the serial monitor should report data from one device and then print "no more addresses" and repeat about every few seconds. If no data is presented then check to be sure the sensor is on D6 and the pull up 4.7K resistor is in place between 3.3VS and D6. Be sure the sensor and resistor wires are firmly seated in the screw terminal and the screw terminal is tightened down.
In most circumstances we want to read multiple DS18B20 sensors on the same bus by their known, pre - determined address in a specific order by distance from the processor. For example, in the past we have used the Panther Logger and multiple DS18B20 devices potted onto a cable to measure water temperature by depth in lakes onboard our Panther Buoy (here). It was necessary in this application, and indeed in most applications to know the position of the sensor in the array.
In the DS18B20_Scan script we can see the address of the sensor reported to the serial monitor window thanks to Paul Stoffregen's code. It is the 64-bit hexadecimal numbers printed after the word "ROM = " in the serial monitor output. Now is a good time to mention that its always a good idea to checkout the datasheet for the device you are using. The datasheet for the DS18B20 can be found here. This sensor was invented by Dallas Semiconductor, now Analog Devices.
With the OneWire library we can search for specifically a device with this address on the OneWire bus (which in this case is digital pin 6) and read its temperature data. If we have mutliple DS18B20 devices then we can store their addresses in an array and then have the processor iteratively search for each of these devices by their address and store the data in a float array.
Navigate back over to our Github library and pull up the DS18B20_Multiple script here. As previous, copy and paste this into a new Arduino script and save it with a new file name.
In this script we moved Paul Stoffregen's code in the loop of the last script into its own function called readTemps. We also define the number of sensors we have with:
#define NSENSORS 5
We are going to pretend we have 5 even though we only have one. We have also created a float object, Temp, that contains enough placeholders for as many temperature values as we have temperature sensors.
float Temp[NSENSORS];
Similarly, we create a byte array that is 8 columns of data corresponding to the 8 parts of the address and as many rows as we have sensors. Again, we only have one, but for demonstration purposes we are going to pretend we have five and just repeat the same address five times. Here's my address repeated five times:
byte Address[NSENSORS][8] = {
{0x28, 0x12, 0x98, 0xAC, 0x0D, 0x00, 0x00, 0x09},
{0x28, 0x12, 0x98, 0xAC, 0x0D, 0x00, 0x00, 0x09},
{0x28, 0x12, 0x98, 0xAC, 0x0D, 0x00, 0x00, 0x09},
{0x28, 0x12, 0x98, 0xAC, 0x0D, 0x00, 0x00, 0x09},
{0x28, 0x12, 0x98, 0xAC, 0x0D, 0x00, 0x00, 0x09}
};
Now notice the "for loop" that was added in the readTemps function. Since we have more than one sensor we need to take each address one-by-one and read temperature data from the corresponding sensor, then that data is put into the same position in the float Temp array until the loop reaches the number of sensors as defined in NSENSORS. For more information on how the very useful for loop function works see the Arduino reference here.
The setup function is the same as previous. We start communications with the MCP and turn on the 3.3V switched rail using pin 4 on the MCP.
In the loop we now just need to run the readTemps function and print the data to the serial monitor. In order to make the code agnostic with respect to the number of sensors we again use a for loop to cycle through each position of data in the Temp float array to print the results from each sensor.
Run the previous DS18B20_Scan script and find the address of your sensor. Copy the address and enter it five times into the byte Address array in the DS18B20_Multiple sketch. See the video to the right to see how this is done. The addresses are entered as hexadecimal numbers. Make sure there is a comma after each of the first four addresses. Then upload this sketch to the Telleloger and open the serial monitor.
Notice that although we are reading the same sensor, we can have different temperatures reported to the serial monitor because the sensor is being read at different times. Natural variation and sensor noise cause this to happen.
If you actually have multiple sensors attached and want to read them in a specific order then you would just put their addresses in the correct order in the Address byte array. The temperature data is saved in a global float array so that it can be used later to send to an IoT database using one of the Panther Logger's communication methods.
Copyright © 2023 ElectroRex - All Rights Reserved.
Powered by GoDaddy
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.