Analogue input into pi


20 posts
by dannyboy » Mon Jun 25, 2012 5:01 pm
Hi, I've been playing around with the GPIO in my pi and managed to get digital inputs and outputs working pretty well, however for my next project (a nano brewery) I'd like to measure temperature using the pi so that I can switch on and off a heating element but also have a live readout of the temperature and make a trend graph. Could I do this with the GPIO or am I on the wrong track, I was thinking of something fairly simple like a resistance thermometer or a thermocouple which I'd calibrate with the software...

Any ideas how I'd get started?
@dannyboy6661984 on Twitter
Posts: 5
Joined: Mon Jun 04, 2012 11:10 am
by domesday » Mon Jun 25, 2012 5:20 pm
There are no analogue inputs on the Pi so you will either have to use an A/D convertor or use a ready made temperature sensor that can talk to the Pi using a serial protocol such as 1-Wire, SPI, I2C.
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK
by dannyboy » Mon Jun 25, 2012 9:49 pm
So I'm looking at something like this?
http://uk.rs-online.com/web/p/temperatu ... r/3683923/
@dannyboy6661984 on Twitter
Posts: 5
Joined: Mon Jun 04, 2012 11:10 am
by Lorian » Mon Jun 25, 2012 10:17 pm
I'd go for something mainstream like DS18B20 or something more accurate. You can get them in waterproof.metal housings for a few pounds/dollars.
Posts: 112
Joined: Sun Mar 11, 2012 10:09 am
by rurwin » Mon Jun 25, 2012 11:05 pm
dannyboy wrote: a resistance thermometer or a thermocouple which I'd calibrate with the software...


Thermocouples measure the difference in temperature between a cold junction and a hot junction. So you need another thermometer to measure the temperature of the cold junction. Consider using a PRT (platinum resistance thermometer) instead. To get decent readings from PRTs or thermocouples you will need some good quality, low noise amplification and filtering before the analogue-digital converter. Of course for the PRT you will need to convert a 100 ohm resistance into a voltage. For a thermocouple you just have to amplify the voltage, but it is very small: 59mV for 1200C.

They are all non-linear, but the conversion formulae are available.

The suggestions above give devices that are accurate to within 0.5 - 1 degree C, which is not enough in a lot of cases. Thermocouples and PRTs with decent electronics can get down to 0.1 degrees C. Getting any better than that is difficult.
User avatar
Forum Moderator
Forum Moderator
Posts: 2932
Joined: Mon Jan 09, 2012 3:16 pm
by jbeale » Mon Jun 25, 2012 11:39 pm
I think a 1-wire sensor (like the DS18B20) or I2C sensor would be the easiest approach. Has anyone reported using 1-wire on the R-Pi yet? I know I2C is working. Using a thermocouple or Platinum RTD could work over a larger temperature range, and the PRT is more accurate if done correctly, but due to the analog signal handling requirements, that approach is not what I would recommend to a beginner. Does a nano-brewery need better than 1 degree C accuracy?
User avatar
Posts: 2076
Joined: Tue Nov 22, 2011 11:51 pm
by jan wagemakers » Tue Jun 26, 2012 6:03 am
dannyboy wrote:I'd like to measure temperature using the pi so that I can switch on and off
a heating element but also have a live readout of the temperature and make a
trend graph. Could I do this with the GPIO or am I on the wrong track,

I have recently connected a microchip TC74¹ to a PIC microcontroller via
I2C. Works very well. I don't have a Raspberry Pi yet, but because the
Raspberry Pi supports I2C, it should be no problem to connect a TC74
directly to the Raspberry Pi.

[1] <https://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010749>
Posts: 3
Joined: Sun Mar 11, 2012 9:24 am
by dannyboy » Tue Jun 26, 2012 6:12 am
This is great guys, exactly what I'm after. I was thinking of a thermocouple or PT100 because that's what I know from work (instrumentation on industrial processes). However for what I'm thinking of doing here, my temperature range is pretty small, probably 15 to 25 degrees C if that much, and an accuracy of +-0.5 degree is probably going to be sufficient for now.
What I'm thinking in a little more detail is having one or possibly a few probes in a 25 litre fermentation bucket which will both be logging data and also switching on and off a relay that will control a heater of some sort, probably a belt heater round the outside.

By the sounds of it, the best thing to do to begin with is get playing with a DS18B20 and once that's working start playing with analogue to digital converters to try and get better accuracy with a resistance thermometer.

Any ideas how I'd get data from the DS18B20? I've been learning Python but would a different language be more suitable?
@dannyboy6661984 on Twitter
Posts: 5
Joined: Mon Jun 04, 2012 11:10 am
by Super-Nathan » Tue Jun 26, 2012 6:18 am
I believe an Arduino connected via USB to the pi might be a much easier and relatively affordable alternative.

For more information on connecting an Arduino to a server and controlling the Arduino Via USB over the internet check out this documentation. they are using an Ubuntu server but that is easily adaptable to the Debian Pi image.

http://www.grovercam.com/build.html
echo "Something Useful" > /dev/null
User avatar
Posts: 67
Joined: Sun Jun 17, 2012 6:56 am
Location: South Korea
by Lorian » Tue Jun 26, 2012 7:34 am
Dont overlook they are available like this for not much money

http://www.ebay.co.uk/itm/Waterproof-Di ... 27c867b174

not necessarily from that seller, that's just the first one my search found.
Posts: 112
Joined: Sun Mar 11, 2012 10:09 am
by rurwin » Tue Jun 26, 2012 8:09 am
Given your background, I'd guess the easiest way to handle it would be a simple I2C ADC and get a PRT with a 4-20mA sender on it. Then you would not need a complex amplification/filtering circuit; 4-20mA can be converted to voltage with just a resistor or two and the sender probably handles low-pass filtering. I think there are I2C drivers in RaspPi already, but it's supported in hardware so even if you have to do it yourself it would be fairly simple. SPI would not be too difficult, but it would limit you to two devices and there are no RaspPi drivers yet IIUC. On the other hand, to use a 1-wire interface you would have to handle all the low-level bit-banging yourself.

I can see lots of I2C temperature sensors. For example TMP102. Most of them can take 3.3V or 5V, and most of them are accurate to 0.5C, but they are all surface mount and I could not find any in an industrial probe form. You could make your own probe, although thermal resistance may be a problem, or just strap it to the outside of the vessel.
User avatar
Forum Moderator
Forum Moderator
Posts: 2932
Joined: Mon Jan 09, 2012 3:16 pm
by jan wagemakers » Tue Jun 26, 2012 9:19 am
Super-Nathan wrote:I believe an Arduino connected via USB to the pi might be a much easier and relatively affordable alternative.

It is indeed possible to connect a temperature sensor to an
arduino/microcontroller and export that over USB¹ to a PC/Raspberry Pi.
But because the Raspberry Pi has GPIO pins, I think it's a waste to use the
USB-port for that.

[1] http://www.janwagemakers.be/wordpress/?p=152>
Posts: 3
Joined: Sun Mar 11, 2012 9:24 am
by peterm2 » Wed Jun 27, 2012 7:52 pm
I notice another project lower down using a Vellerman k8055 Board. It has two analog inputs.
Posts: 19
Joined: Wed Jun 27, 2012 7:46 pm
by davidmam » Wed Jun 27, 2012 8:55 pm
Try this thread: viewtopic.php?f=44&t=6649&hilit=1wire

It works fine. Reading the temperature from the 1-wire with python is trivial
Simple cgi script to push a list of sensors and temperatures onto a simple web page.

Code: Select all
#!/usr/bin/python

import cgi
import os
import re

temps={}
path="/sys/bus/w1/devices"
devlist=os.listdir(path)
for dev in devlist:
  if dev !="w1_bus_master1" and dev !=".":
    tf=open("%s/%s/w1_slave"%(path, dev))
    null=tf.readline()
    temp=tf.readline()
    r=re.match(r'.*t=(\d+)$', temp)
    if r:
      temps[dev]=float(r.group(1))/1000
    tf.close()

print "Content-Type: text/html"
print
print "<html><head><title>Temperature</title></head><body><h1>Temperature</h1>"
print "<dl>"

for t in temps:
  print "<dt>%s</dt><dd>%s C</dd>"%(t,temps[t])
print "</dl></body></html>"
Posts: 98
Joined: Tue Dec 06, 2011 4:13 pm
by jan wagemakers » Sun Jul 01, 2012 9:58 am
Now that I have my Raspberry Pi, I have connected a TC74 temperature sensor via I²C to the Raspberry Pi.
http://www.janwagemakers.be/wordpress/?p=214
Posts: 3
Joined: Sun Mar 11, 2012 9:24 am
by Sjöström » Thu Jul 05, 2012 8:27 am
I use game controllers to get analogue input. The analogue joysticks are just variable resistors so by replacing them with resistive sensors, readings appears as axis movements from the event device, supported by the kernel. One sensor per axis will give a decent setup, but by narrowing the sensors interval with resistors, it's possible to use one axis for several sensors.

Of course, this method occupies a USB port, but it's pretty neat, you can recycle old forgotten controllers, and it will even make your sensors wireless (by using wireless xbox controllers for example).

I've seen discussions about using the GPIOs for mouse and keyboard input http://www.raspberrypi.org/phpBB3/viewtopic.php?f=50&t=8842 and I suppose joystick input might be a possible development, even if doubt there's anything to gain in bypassing the USB interface.
Posts: 2
Joined: Thu Jul 05, 2012 7:35 am
by morphy_richards » Thu Jul 05, 2012 8:48 am
Sjöström wrote:I use game controllers to get analogue input. The analogue joysticks are just variable resistors so by replacing them with resistive sensors, readings appears as axis movements from the event device, supported by the kernel. One sensor per axis will give a decent setup, but by narrowing the sensors interval with resistors, it's possible to use one axis for several sensors..

Am I understanding you right ... you are replacing the variable resistors with some sensors in the joystick. The joystick includes it's own analogue to digital converter for each axis. (so 2 ADCs?) And then this feeds to a USB and is in a "game controller" format which Linux on Pi will easily accept?
So something like this low cost analogue game pad would give 4 analogue inputs would also work?
Would you be able to add more sensors just by using a powered USB hub?

This is great! Thanks for posting this!
User avatar
Posts: 874
Joined: Mon Mar 05, 2012 3:26 pm
Location: London
by Sjöström » Thu Jul 05, 2012 10:18 am
Am I understanding you right ... you are replacing the variable resistors with some sensors in the joystick. The joystick includes it's own analogue to digital converter for each axis. (so 2 ADCs?) And then this feeds to a USB and is in a "game controller" format which Linux on Pi will easily accept?
So something like this low cost analogue game pad would give 4 analogue inputs would also work?
Would you be able to add more sensors just by using a powered USB hub?

This is great! Thanks for posting this!


That's right. I'm actually leaving the variable resistors in place and just solder in the sensor wires. The "joysticks" are still useful for calibrating measuring ranges and testing as you easily get max and min values by moving the stick.
In The Pi , the controller appears as a regular event device (evdev kernel module) and shows up on lsusb listing. Devices used are usually /dev/input/js[0-X] for joystick input or /dev/input/event[0-X] for force-feedback motor control (yes, you can control motors and relays with those).
There's some explaining source code in the "joystick" debian-src package, especially jstest.c and fftest.c
I have only used the old xbox controller, which is well supported in kernel, but I suppose the Gameprince JH 501 is based on standard tech even if it's not listed in any hardware compatibility db. You'll have to test it before you know, as standard procedure with most chinese low cost gadgets.
In my setup: 1 controller with 6 sensors (2 per joystick and 1 per trigger) and two relays controlling 12V external power, it doesn't draw any noticable current so I use unpowered hub, and there's no voltage drop at all. If you are using the force feedback device for powering motors or are using a wireless controller, I would recommend powerd hub. The evdev doesn't have a limit for how many controllers you can use (or maybe 255) so I guess that's up to other limitations.
Posts: 2
Joined: Thu Jul 05, 2012 7:35 am
by iinnovations » Thu Jun 06, 2013 5:18 pm
I know this is old, but use a DS9490R with a DS18B20 network and install owfs. Done.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com
User avatar
Posts: 243
Joined: Thu Jun 06, 2013 5:17 pm
by danjperron » Sun Jun 09, 2013 12:26 pm
HI there is plenty of examples on old post about the ds18b20 and how to connect the sensor directly to the Rpi.

I used it this winter to protect my pond to freeze. I had 5 sensors in 1 wire mode connected.

http://www.raspberrypi.org/phpBB3/viewtopic.php?f=37&t=24115&p=242573#p242573
http://brewpi.com/
http://blog.turningdigital.com/2012/09/raspberry-pi-ds18b20-temperature-sensor-rrdtool/
Daniel
Posts: 945
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada