We learn how to use the PICAXE to control a family of devices that support bidirectional communication with an MCU using a single wire.
In part 1 of this mini-series, I provided an overview of the PICAXE system and did some BASIC programming of my chip. In part 2, we looked at using the simulate" and debug functions, and we considered the analog-to-digital converter, pulse-width modulation, and the one-line display options offered by the PICAXE. I also mentioned the Dallas-Maxim 18B20 digital temperature sensor, and we discussed how the PICAXE's READTEMP command makes using this sensor really easy.
In fact, the DS18B20 is just one of a family of one-wire devices that support bidirectional communication with an MCU using a single wire. They include sensors, EEPROMs, digital potentiometers, and serial number chips, including the Button devices that look like watch batteries (more on these later). The PICAXE makes it easy to use all these devices with the OWIN, OWOUT, and read serial number (READOWSN) commands, but you would still require four lines of code just to get the basic 12-bit value out of a DS18B20. The READTEMP command does all this in one line, along with truncating the 12-bit resolution (0.0625-degree) value to a handy eight-bit (one-degree) resolution value. You can also access the full 12-bit value using the READTEMP12 command.
Thus, my first task was to hook a DS18B20 up to my PICAXE and start reading it. Yet again, I proved that electronics offers unparalleled opportunities for making a chump of oneself. I failed to notice that the DS18B20 requires a 4.7KΩ pullup resistor on the data line. This is clearly shown in the schematic diagram on the READTEMP page in the manual, along with a pinout of the IC. My reluctance to read the farcical manual meant that I kept getting a temperature of 0 degrees. Along the way, I moved one of the connections, which meant I still failed to get a reading when I finally put in the 4.7KΩ resistor. Finally, I sorted things out and managed to obtain a temperature reading of 24 degrees. The DS18B20 reads in Celsius (Centigrade), which is fine for us civilized types. If you want your readings in Fahrenheit, you will have to perform a conversion in software.
The DS18B20 offers some fancy features, including alarms and built-in serial numbers, so that you can put many of them on one line and address them individually. However, the basic READTEMP and READTEMP12 commands don't access these features. You could do so yourself using the one-wire commands if you wish. Of course, the huge bonus with the DS18B20 is that, being digital, you can put it on the end of a long wire. As long as you can still talk to it, you won't have any worries about analog voltage drop or noise altering your reading.
I have a couple of indoor-outdoor electronic thermometers that I bought at a Tandy in Paris years ago when I found them on special offer. I wondered if I could duplicate their functions -- such as stored minimum and maximum readings -- with my DS18B20s. Once I got one connected correctly, it was a simple matter to connect the second and get my display to give me two readings.
The accuracy of the DS18B20 is half a degree. I switched from READTEMP to READTEMP12 to obtain the full 12-bit value. Initially, I just displayed the decimal version of the 12 bits. The readings from the two sensors were always within one count of each other. The format of the 12-bit data -- once you have it in a word variable -- is SSSSSTTT TTTTFFFF, where S is the sign bit (1 = negative), T is the integer of the temperature (you get this part with the basic READTEMP command), and F is the fractional part (one count = 1/16 degree = 0.0625 degrees). However, this value is in two's complement format, so negative temperatures tend to look very strange.
It's been a long time since I had anything to do with two's complements, so a reference to Max Maxfield's excellent book Bebop to the Boolean Boogie was necessary. This told me that I had to take the TTT TTTTFFFF portion of the value and subtract it from 1000 00000000 to get the number right. I soon had my system displaying two temperatures. I got the FFFF part to display 0.X degrees by dividing it by 25, which isn't absolutely correct, but it's near enough for what I wanted.
As you can see below, I've mounted my PICAXE board, the OLED display, and a small breadboard on an old modem case to make it a bit easier for me to play around with things.
Two things are apparent from the above image. First, I'd just zapped the Temp1 sensor with Freezit Spray; that's why this sensor is displaying such a low value. Second, my breadboards are not as bodacious as Max's. (See: The Awesome Art of Bodacious Breadboards.)
To Page 2 >