Matt1234
Posts: 33
Joined: Mon Aug 06, 2012 12:34 pm

A - D converters...

Thu Aug 09, 2012 2:19 pm

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

Matt1234
Posts: 33
Joined: Mon Aug 06, 2012 12:34 pm

Re: A - D converters...

Thu Aug 09, 2012 2:22 pm

If no then does anybody know where to find some example code... Would be extremely grateful! :)

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am

Re: A - D converters...

Thu Aug 09, 2012 4:35 pm

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.

User avatar
gordon@drogon.net
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: A - D converters...

Thu Aug 09, 2012 7:01 pm

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

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:

Image

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/

User avatar
Grumpy Mike
Posts: 1005
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District

Re: A - D converters...

Thu Aug 09, 2012 11:51 pm

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!
I will have some sample code using this chip by the middle of next week if you want to hang on.
It is a simple I2C device.

User avatar
Grumpy Mike
Posts: 1005
Joined: Sat Sep 10, 2011 7:49 pm
Location: English Lake District

Re: A - D converters...

Mon Aug 13, 2012 11:10 am

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

Code: Select all

# don't blacklist spi and i2c by default
# blacklist spi-bcm2708
# blacklist i2c-bcm2708
then you have to add a line to /etc/modules to make it look like this:

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

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

User avatar
peepo
Posts: 308
Joined: Sun Oct 21, 2012 9:36 am

Re: A - D converters...

Wed Sep 11, 2013 1:39 pm

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

User avatar
gordon@drogon.net
Posts: 2024
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK

Re: A - D converters...

Wed Sep 11, 2013 2:45 pm

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
The pcf8591 is the chip used in the Quick2Wire boards.

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)) ;
}
Simples.(just remember to load the I2C kernel module: 'gpio load i2c' and off you go)

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/

User avatar
joan
Posts: 16214
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: A - D converters...

Wed Sep 11, 2013 2:57 pm

C and Python examples of reading a board incorporating that chip are in http://www.raspberrypi.org/phpBB3/viewt ... 91#p391450

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: A - D converters...

Sun Dec 01, 2013 12:49 pm

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

ondrej
Posts: 34
Joined: Fri Jul 31, 2015 11:14 am

Re: A - D converters...

Tue Sep 08, 2015 9:51 pm

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?

ondrej
Posts: 34
Joined: Fri Jul 31, 2015 11:14 am

Re: A - D converters...

Mon Jul 25, 2016 5:49 pm

ondrej wrote:however I am getting as example following values: 96 211 91 255.
These are values of all 4 analog channels of PCF8591 (IN0-IN3)

Return to “HATs and other add-ons”