04 May, 2019

Using Go to read the temperature from R1820 on a Raspberry Pi running DietPi

The other day, I came across an electronic component labeled with 'RW1820'. The component is a digital temperature sensor. I figured it would fun to see if I could write a program in Go to read the value from the sensor and to display the temperature onscreen.

The Component



Wiring it Up

The little breakout board that the chip is attached to has three pins labeled 'G', 'R', and 'Y'. The 'G' pin goes to ground, the 'R' pin goes to +5V DC, and the 'Y' pin goes to Signal. In my case, the signal is pin 7 on the Raspberry Pi.


Enable 1-Wire

From what I have been able to learn the sensor uses 1-Wire for the communications.

I am running DietPi on the Raspberry Pi so I could not use the standard 'raspi-config' or the 'Raspberry Pi Configuration' in Raspbian. This left me with the option of adding

dtoverlay=w1-gpio

to the '/boot/config.txt' file. Just a quick edit of a file and I should be good to go. It turns out that on DietPi you need to edit the file '/DietPi/config.txt' in order to have the change survive a reboot.

Once dtoverlay is added to 'DietPi/config.txt' file, the Rasberry Pi will start communicating with the device. Take a look in the folder '/sys/bus/w1/devices'. One of the folders will start with '28-' that folder will contain a file name 'w1-slave' if you read the content of that file you will get something like

72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
72 01 4b 46 7f ff 0e 10 57 t=23125

Notice the 't=' at near the end of the second line. This is the temperature in Celsius. This temperature is also missing a decimal point.

Go Code


Misc

The RW1820 is very similar to the DS18B20. There is a lot more information about the DS18B20. 

Resources

Rayway International - Chinese
RW1820 Datasheet - English
1-Wire
Enable 1-Wire Interface on the Raspberry Pi
DietPi
W1-GPIO - One-Wire Interface
Introduction to DS18B20

No comments:

Challenging myself to learn something new

I have recently set a big challenge for myself. I want to know about Machine Learning . To add to the challenge, I am trying out usin...