Using the Hobby Boards 6-channel 1-wire master hub with Raspberry Pi

The Raspberry Pi and the 1-wire master hub
If you have the old Hobby Boards 6-channel 1-wire master hub (with the serial cable) you can make it work with your brand new Raspberry Pi quite easily! In this tutorial I'll describe how to modify and set up the master hub so you can read DS1820 temperature sensor values with Digitemp software on the Raspberry Pi.

Disclaimer:
If you decide to follow this tutorial, you do it at your own risk. I am not responsible for any damage you may cause by following this tutorial. Be careful when soldering and connecting wires, and always double check your connections before powering the devices on. (I thought I just had to add this after almost frying my own Raspberry :) )

Short version

Because the old master hub already uses a serial connection, it's basically just a case of removing the level shifting MAX232 chip from the hub, drop the 5 volt voltage coming from the DS2480B to 3.3 volts which the Raspberry likes, and connect your Raspberry to the DS2480B adapter.

Longer and more detailed version

Read this if you didn't quite understand what the short version stated.

Hardware

The Raspberry doesn't need any modding, just a connector for three cables (ground, rx and tx) that you can plug into the gpio connector pins. I used an old connector with cables salvaged from an old pc. The connectors that you used to plug in status leds, reset and power buttons from the computer case to the motherboard fit perfectly.

Just make sure you put them onto the right pins on the gpio connector of the Raspberry Pi. There is a number of sites that display the pinout of the Raspberry gpio connector. If you look at Hobbytronics' page, the needed pins are:

  • pin 6, ground
  • pin 8, tx (also called gpio 14)
  • pin 10, rx (also called gpio 15)

Pins 8 and 10 form the send and receive pins of the Raspberry's serial port. Ground pin is also needed for the connection to work.

Closeup view of the modded 1-wire hub
The 6-channel master hub needs a bit more of modding to work. Begin work with desoldering the serial cable. Cut off the zip tie that secures the cable and desolder the three wires (see the yellow circular area at the left in the image). The (green) wire at position 1 is ground, and you can solder your own ground wire in the same connection point. Your new ground wire will connect to pin 6 on the Raspberry Pi.

Next you can remove the level shifting MAX232 chip that sits just right of the serial cable connection (see image). The chip was needed to adjust the different voltage levels coming from the pc serial port to fit the TTL levels on the rest of the boards components. Because the Raspberry already uses TTL level signals Wrong! The Raspberry uses 3.3 volt signals!, we don't need the level shifter anymore. Desoldering the chip is a bit tricky because it's a very small chip with lots of legs. But with careful use of the soldering iron and perhaps some solder wick, the chip finally comes off (in one or more pieces).

Make sure you don't solder together any pads so they don't short circuit anything critical on the board.

When the MAX232 has come off, it's time to find the DS2480B adapter. It sits just above the MAX232 (see image). This chip is really tiny, and you need to solder two wires onto it's legs. If you find soldering onto the chip's legs impossible, you can also solder the two wires to the empty pads where the MAX232 sat earlier. Looking closely at the copper leads that run from the DS2480B's pins 7 and 8, you can see which pads connect to them. The DS2480B pins are:

  • pin 7, tx (goes to Raspberry pin 8, tx)
  • pin 8, rx (goes to Raspberry pin 10, rx, via a 5v to 3.3v voltage divider)

Note that you have to connect DS2480 rx to Raspberry rx, and not rx-tx as you maybe are used to do generally.

Before you can connect the DS2480B's rx pin (pin 8) to the Raspberry's rx pin (pin 10) you have to reduce the 5 volt voltage coming from the DS2480B to 3.3 volts which the Raspberry tolerates. If you don't do this you are in risk of frying your Raspberry! For the voltage divider circuit you need one 10k resistor and one 20k resistor.

In the photo below you can see how I added the voltage divider to the 1-wire hub using three 10k resistors.

1-wire hub with voltage divider
 
And below you can see the complete wiring circuit diagram (click on the image for a bigger version). The Raspberry "IC" on the left pictures the gpio port of the Raspberry, and the area inside the red rectangle pictures the 1-wire hub board.
Complete circuit

Check your connections with a multimeter before actually connecting them to the Raspberry to avoid short circuits and other problems.

When everything is ready you can connect the 1-wire hub to the Raspberry Pi. The wires connect like this:

  • Raspberry pin 6 -> hub ground
  • Raspberry pin 8 -> DS2480B pin 7 (tx)
  • Raspberry pin 10 -> voltage divider -> DS2480B pin 8 (rx)

Software installation

Start software installations with disabling the serial console that uses the Raspberry's serial port because we need the port for communicating with the 1-wire hub. Raspberry Pi projects site has a very good tutorial about how to do this, so I won't repeat it here. Read the instructions in the chapter "Turning off the UART functioning as a serial console". If you don't do this, the serial console will interfere and cause problems with reading the 1-wire hub.

Next, install Digitemp. This can be done with the command:

sudo apt-get install digitemp

The Raspberry's serial port we're using is called /dev/ttyAMA0. After installing digitemp, you have to configure it by commanding:

sudo digitemp_DS9097U -i -s /dev/ttyAMA0 -c /home/pi/digitemp.conf

This will make digitemp search for 1-wire devices on the serial port ttyAMA0 and write a configuration file called digitemp.conf in pi's home folder. If you get errors (Error 5 no DS2480B adapter detected and error 10 Read COM failed) check your connections and try powering the 1-wire hub off and back on again.

Finally you can read the temperature sensors by commanding:

sudo digitemp_DS9097U -a -s /dev/ttyAMA0 -c /home/pi/digitemp.conf

This command would give you a result that looks something like this:

May 24 21:25:43 Sensor 0 C: 23.66 F: 74.59

Reading just one sensor (sensor 0) goes like this:

sudo digitemp_DS9097U -t 0 -q -c /home/pi/digitemp.conf

As Paul wisely recommended you might want to use digitemp without sudoing as root the whole time, so you can simply add user pi to the tty group by typing:

sudo usermod -G tty pi

Example usage of Digitemp (automated temperature logger using Thingspeak.com)

If you want you could turn your 1-wire enabled Raspberry (with a working Internet connection) to an automated temperature logger using the Thingspeak data logging and graphing service. Here's how to do it.

Start with creating a user account at Thingspeak, and then log in to the site. Go to the channels page, where you'll find a "Create new channel" button. Create a new channel by clicking the button and fill in the channel info form. You only need to fill in the channel name and some name for Field 1 (assuming that you have only one sensor's data to post). Also remember to check the "Make public" option so you can see the graph without passwords. See example image below.

Thingspeak channel settings


Next, click on the API keys tab and copy the write API key which consists of capital letters and numbers. You'll need this key when posting the sensor data to the service.

Thingspeak api key

In order to strip Digitemp's output to the bare minimum (just the temperature reading in Celsius), you have to edit the digitemp.conf file a bit. Open the file with a text editor and look for the row that begins with LOG_FORMAT. The original row looks something like this:

LOG_FORMAT "%b %d %H:%M:%S Sensor %s C: %.2C F: %.2F"

Now edit the row to look like this:

LOG_FORMAT "%.2C"

This will remove everything else (date, time etc.) from the output, and leave only the temperature reading in Celsius. Save the file and try reading the sensor:

sudo digitemp_DS9097U -t 0 -q -c /home/pi/digitemp.conf

The result should now look something like this:

23.88

Now we write a small shell script that posts the sensor data to Thingspeak. Create a new file (in user pi's home folder: /home/pi) by typing:

nano mytempscript

Write the following code in the file:

#!/bin/bash
temp=$(digitemp_DS9097U -q -t 0 -c /home/pi/digitemp.conf)
url="http://api.thingspeak.com/update?key=C49Y9U32YH8F1DN1&field1=$temp"
wget $url -q -O /dev/null

Remember to replace the API key with your own, so your sensor data doesn't end up in my channel :)

Save the file (ctrl-O) and exit nano (ctrl-X). After saving the file you need to give the script execution rights by typing:

chmod a+x mytempscript

Now you can try the script by typing:

sudo ./mytempscript

The sensor reading takes a while (about a second) and you should see the red led flashing once on the 1-wire hub when the Raspberry performs the reading operation. If you got no errors, the reading should appear on Thingspeak's graph as a red dot like in the image below:

Thingspeak data received

Finally you can automate the reading process by adding the script execution to crontab. Open up crontab by typing:

sudo nano /etc/crontab

This will bring up the scheduled task file to which we will add our own script to be executed periodically. The bottom part of the file will look something like this:

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
#


To add our own script we edit the file to look like this:

# m h dom mon dow user  command
*/5 *   * * *   root    /home/pi/mytempscript
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --repo$
#


As you can see, we have added a row where our script is executed every five minutes (*/5). Check here to see more examples of how to edit crontab.

Now we just sit and wait for the script to execute a few times (once every five minutes here) and check the Thingspeak graph for more appearing red dots. And after some time the graph will hopefully look something like this:

Thingspeak populated graph


Tip! Martybugs site has more info about using digitemp.

And that's all folks, have fun!


Credits
Thanks to Paul @ www.sheepwalkelectronics.co.uk for bugfixes and valuable comments!

Comments