12 May, 2019

Today on the bench, I have the HC-SR04 Ultrasonic Range Sensor connected to a Raspberry Pi 3 Model B running DietPi.



The HC-SR04 sensor has four pins (VCC, Trig, Echo, Gnd) that need to be connected to the Raspberry Pi. VCC gets connected to one of the 5v pins and Gnd (ground) gets connected to on the of the ground pins on the Rasberry Pi. This leaves Trig (Trigger) and Echo to be connected to the Rasberry Pi.

There is a minor point that needs to be considered before connecting the Trig and Echo pins to the Raspberry Pi. The GPIO headers on the Raspberry Pi can only safely handle 3.3v. The sensor is going to send the signal back using 5v. I need a way to bring 5 volts down to something under 3.3 volts. One way to do this is with a voltage divider. All I need is a couple of resistors, jumper wires, and some math. The math will be handled by the Voltage Divider Calculator. In my stock of resistors, I can see that I have a lot of 1K resistors. I will use one of those. Into the voltage divider calculator goes 5v for the voltage source, 3.3v for the output voltage, and 1K for resistance 1. The results of the calculation gave me a value of just under 2K for resistance 2.

I connected the Trig pin on the sensor to one of the open BCM pins. Now for the tricky part, I connected ground to one leg of the 2K resistor. The other leg connects to a jumper wire going to an open BCM pin on the Raspberry Pi. I then connected the Echo pin of the sensor to one leg of the 1K resistor. The other leg of the 1K resistor gets connected to the jumper wire that I connected the 2K resistor to.





This takes care of the wiring up of the sensor. Now we get to move on to writing some code.

Over in GitHub, I created a repo named go_rpi. In the devices folder, there is a file named 'hcsr04.go'. This code file contains the code for interacting with the HC-SR04 sensor. I am taking advantage of the go-rpio library from Stian Eikeland to access the GPIO-pins. The heart of the code is the 'measure' function. This function coordinates the actions for initializing the pins to a low state, triggering the pulse, capturing the time it takes the signal to go out and back, and calculating the distance.

Resources

Datasheet on HC-SR04

Voltage Divider

Blog Posts

GitHub







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

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...