The second part of this week’s 2-Bullet Tuesday! See the first part here.
You can subscribe to the newsletter on the 2-Bullet Tuesday page!

Omega Tip of the Week: Using a 1-Wire Temperature Sensor

This week we’ll cover how to read the ambient temperature using the popular (and affordable) DS18B20 1-Wire temperature sensor! We’ll first wire up the sensor to our Omega, register a software 1-Wire bus master, and finally, read the temperature data.

The temperature sensor, like most 1-Wire devices, has three available connectors: the power, ground, and data line. Treat the side with the flat part as the front and wire it up like so:

  • Connect the ground (left pin) to the Omega’s ground.
  • Connect a 4.7kΩ pull-up resistor between the the data line (middle pin) and power pin (right pin).
  • Connect the data line to a GPIO on the Omega; we’ll be using GPIO19 for this example.
  • Provide 3.3V to the power pin.

Your setup should look something like this:

So we have our 1-Wire slave device connected to the Omega, what now? Well, 1-Wire is a master-slave communications bus and so the Omega will need to become the 1-Wire bus master. The Omega has dedicated hardware for several types of buses (I2C, SPI, I2S), but not for 1-Wire. So instead we’ll use software to imitate a 1-Wire bus master on one of our free GPIOs:

insmod w1-gpio-custom bus0=0,19,0

We’re telling Linux to load the w1-gpio-custom module and to setup bus 0 to use GPIO19 in non-open drain mode.

If everything worked properly, you’ll now be able to access to the /sys/devices/w1_bus_master1 directory. This directory will be our 1-Wire command centre!

To check how many slave devices are connected, we can cat /sys/devices/w1_bus_master1/w1_master_slave_count, and to find out the unique ID of each connected device, cat /sys/devices/w1_bus_master1/w1_master_slaves

1-Wire sensors all have a unique ID that is used when communicating with bus masters. While your sensor’s unique ID will be, well, unique, the command to read from it will follow this pattern:

cat /sys/devices/w1_bus_master1/(UniqueDeviceID)/w1_slave

It turns out our DS18B20 sensor’s unique ID is 28-041686e9d1ff. So we can run the following command to read data from the sensor:

root@Omega-7ADD:~# cat /sys/devices/w1_bus_master1/28-041686e9d1ff/w1_slave
7f 01 4b 46 7f ff 0c 10 ba : crc=ba YES
7f 01 4b 46 7f ff 0c 10 ba t=23937

The command shows us all of the output from the sensor, but we’re only interested in the t=23937 part that indicates the temperature is 23.937˚C.

Let’s do some string manipulation to isolate the temperature reading:

root@Omega-7ADD:~# awk -F= '/t=/ {printf "%.03f\n", $2/1000}' /sys/devices/w1_bus_master1/28-041686e9d1ff/w1_slave
23.875

And that’s it! For more info on 1-Wire and the Omega, check out the 1-Wire article in our documentation and Wikipedia for more info on the 1-Wire protocol.

 

Let us know what kind of stuff you would like to see featured on 2-Bullet Tuesday! Send a tweet to @OnionIoT with your suggestions!

Thanks for reading! Have a great week!

Team Onion

 

 

P.S. — Our Omega2 Indiegogo campaign will be ending in the near future! Don’t miss your chance to get the Omega2 for just $5!