Recently I bought an PCF8591 from eBay. It is an I2C compatible analog to digital converter.
Truth be told I have no idea how to program it in python! Because the gertboard has some analog to digital converters will there be a wealth of tutorials in how to use my dormant a/d converter... Really want to read the value of a potentiometer! Thanks
Re: A - D converters...
If no then does anybody know where to find some example code... Would be extremely grateful! 

- Gert van Loo
- Posts: 2487
- Joined: Tue Aug 02, 2011 7:27 am
Re: A - D converters...
All Gertboard learn/demo programs are all available from the Farnell website. I have seen links to the documentation which bypass the Farnell login. I will see if I can get a link to the TAR file a well.
Otherwise there is the original Gertboard demo code which has been available for months.
Otherwise there is the original Gertboard demo code which has been available for months.
- gordon@drogon.net
- Posts: 2024
- Joined: Tue Feb 07, 2012 2:14 pm
- Location: Devon, UK
Re: A - D converters...
I don't know about the I2C devices you mention, but I've written some code and incorporated it into wiringPi for the Gertboard's SPI A/D and D/A devices.Matt1234 wrote:Recently I bought an PCF8591 from eBay. It is an I2C compatible analog to digital converter.
Truth be told I have no idea how to program it in python! Because the gertboard has some analog to digital converters will there be a wealth of tutorials in how to use my dormant a/d converter... Really want to read the value of a potentiometer! Thanks
And example program is here:
http://unicorn.drogon.net/gertboard.c
that outputs a sine wave on one pin, then read analog in on one of the input pins, then sends it back to the 2nd output - I then connected a 'scope to both output pins just to see what was going on:

Looks fine to me

This all uses the standard SPI kernel driver rather than bit-banging the hardware directly which Gerts original code does - I feel this is the best way forward though, but I know that when Gert write his original code the kernel driver wasn't avalable!
The kernel driver is stable now - or seems to be to me - I've tested it with this and the PiFace IO expander board with good results, so I see no reason not to use it.
I'll be doing one or 2 more tests tonight & tomorow, but will have a new version of wiringPi up at the weekend with all this code in it, also a new version of my gpio command-line program to make it easy to load the SPI (and I2C) modules and set the right permissions so user-land code can use the SPI (and I2C) devices without needing to use sudo or run them as root.
As for Python - Don't know, I'm not a Python programmer, but there is a chappie who's busily taking my wiringPi and producing Python wrappers for all of it, however if there is an existing Python SPI driver then it will probably "just work", once you know the right commands to poke at the chips (see Gerts or my code for that, or just get the data sheets!)
But reading a pot is easy - the A/D input is 0-3.3v, so connect a 10K pot from 0v to 3.3v with the wiper going directly to the input pin. It will read from 0-1023.
-Gordon
--
Gordons projects: https://projects.drogon.net/
Gordons projects: https://projects.drogon.net/
- Grumpy Mike
- Posts: 1005
- Joined: Sat Sep 10, 2011 7:49 pm
- Location: English Lake District
Re: A - D converters...
I will have some sample code using this chip by the middle of next week if you want to hang on.Matt1234 wrote:Recently I bought an PCF8591 from eBay. It is an I2C compatible analog to digital converter.
Truth be told I have no idea how to program it in python!
It is a simple I2C device.
- Grumpy Mike
- Posts: 1005
- Joined: Sat Sep 10, 2011 7:49 pm
- Location: English Lake District
Re: A - D converters...
First you have to get the I2C drivers into the kernel. I uses the raspbian distro which already has it in. Unfortunately it is disabled so you have to un comment the two lines in:-
/etc/modprobe.d/raspi-blacklist.conf
which stop needed modules from loading,
the file will then look like this:-
then you have to add a line to /etc/modules to make it look like this:
Then you need to install the I2C smbus drivers as explained here
http://www.raspberrypi.org/phpBB3/viewt ... 44&t=13988
Finally you can write code to access the chip. This code simply reads channel 0 and prints it out if it has changed.
Note the other channels should be connected to ground. Channel 0 is connected to the wiper of a pot, 10K should do. The ends of the pot are connected to +5V and ground.
The chip should be wired up to +5V and ground with data and clock to GPIO 0 & 1
/etc/modprobe.d/raspi-blacklist.conf
which stop needed modules from loading,
the file will then look like this:-
Code: Select all
# don't blacklist spi and i2c by default
# blacklist spi-bcm2708
# blacklist i2c-bcm2708
Code: Select all
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
i2c-dev
http://www.raspberrypi.org/phpBB3/viewt ... 44&t=13988
Finally you can write code to access the chip. This code simply reads channel 0 and prints it out if it has changed.
Code: Select all
#Read a value from analogue input 0
#in A/D in the PCF8591P @ address 0x48
from smbus import SMBus
bus = SMBus(0)
print("Read the A/D")
print("Ctrl C to stop")
bus.write_byte(0x48, 0) # set control register to read channel 0
last_reading =-1
while(0 == 0): # do forever
reading = bus.read_byte(0x48) # read A/D
if(abs(last_reading - reading) > 2):
print(reading)
last_reading = reading
The chip should be wired up to +5V and ground with data and clock to GPIO 0 & 1
Re: A - D converters...
PCF8591 has 4 analogue inputs AIN0-AIN4
but how does one read more than one?
0x48 reads AIN0
something short in 'C' would suit,
python also good,
though on the evidence of youtube
http://www.youtube.com/watch?v=LMqtVnu6MeU
part2 12:50 shows 0-255 values scrolling by
the speed is quite slow ~20hz max?
why would that be?
why do the values appear to increment-decrement by 3?
eg:
11
14
...
17
14
but how does one read more than one?
0x48 reads AIN0
something short in 'C' would suit,
python also good,
though on the evidence of youtube
http://www.youtube.com/watch?v=LMqtVnu6MeU
part2 12:50 shows 0-255 values scrolling by
the speed is quite slow ~20hz max?
why would that be?
why do the values appear to increment-decrement by 3?
eg:
11
14
...
17
14
- gordon@drogon.net
- Posts: 2024
- Joined: Tue Feb 07, 2012 2:14 pm
- Location: Devon, UK
Re: A - D converters...
The pcf8591 is the chip used in the Quick2Wire boards.peepo wrote:PCF8591 has 4 analogue inputs AIN0-AIN4
but how does one read more than one?
0x48 reads AIN0
something short in 'C' would suit,
python also good,
though on the evidence of youtube
http://www.youtube.com/watch?v=LMqtVnu6MeU
part2 12:50 shows 0-255 values scrolling by
the speed is quite slow ~20hz max?
why would that be?
why do the values appear to increment-decrement by 3?
eg:
11
14
...
17
14
To use it from C/C++ with wiringPi:
Code: Select all
#include <wiringPi.h>
#include <pcf8591.h>
main ()
{
wiringPiSetup () ;
pcf8591Setup (200, 0x48) ;
for (;;)
printf ("%4d %4d %4d %4d\n", analogRead (200), analogRead (201), analogRead (202), analogRead (203)) ;
}
the increment/decrement thing is noise. You can read lots of samples and run a rolling average to dampen it out (digital low-pass filter)
More here: http://wiringpi.com/extensions/i2c-pcf8591/
-Gordon
--
Gordons projects: https://projects.drogon.net/
Gordons projects: https://projects.drogon.net/
Re: A - D converters...
C and Python examples of reading a board incorporating that chip are in http://www.raspberrypi.org/phpBB3/viewt ... 91#p391450
- Richard-TX
- Posts: 1549
- Joined: Tue May 28, 2013 3:24 pm
- Location: North Texas
Re: A - D converters...
Some of the previous code does not work with B series Pis. Here is the updated code.
Code: Select all
#!/usr/bin/python
import smbus
import time
bus = smbus.SMBus(1)
aout = 0
while True:
for a in range(0,4):
bus.write_byte_data(0x48,0x40 | ((a+1) & 0x03), 0)
v = bus.read_byte(0x48)
print v,
time.sleep(0.1)
print
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip
Re: A - D converters...
sorry to hijack the thread. I am using code above to get values from smoke sensor MQ-2 having that interfaced over PCF8591. All works fine, however I am getting as example following values: 96 211 91 255.
First value is getting changed every time MQ-2 captures any kind of detection (alcohol, smokes, gas) Second and third value moves a bit but not so significant, last one never changes.
I was assuming, that I will get different values for different types triggers. Or there is different approach?
can someone explain that to me?
First value is getting changed every time MQ-2 captures any kind of detection (alcohol, smokes, gas) Second and third value moves a bit but not so significant, last one never changes.
I was assuming, that I will get different values for different types triggers. Or there is different approach?
can someone explain that to me?
Re: A - D converters...
These are values of all 4 analog channels of PCF8591 (IN0-IN3)ondrej wrote:however I am getting as example following values: 96 211 91 255.