Tutorial – Arduino and PCF8591 ADC DAC IC

Learn how to use the NXP PCF 8591 8-bit A/D and D/A IC with Arduino in chapter fifty-two of my Arduino Tutorials. The first chapter is here, the complete series is detailed here.

Updated 17/06/2013

Introduction

Have you ever wanted more analogue input pins on your Arduino project, but not wanted to fork out for a Mega? Or would you like to generate analogue signals? Then check out the subject of our tutorial – the NXP PCF8591 IC. It solves both these problems as it has a single DAC (digital to analogue) converter as well as four ADCs (analogue to digital converters) – all accessible via the I2C bus. If the I2C bus is new to you, please familiarise yourself with the readings here before moving forward.

The PCF8591 is available in DIP form, which makes it easy to experiment with:

pcf8591

You can get them from the usual retailers. Before moving on, download the data sheet. The PCF8591 can operate on both 5V and 3.3V so if you’re using an Arduino Due, Raspberry Pi or other 3.3 V development board you’re fine. Now we’ll first explain the DAC, then the ADCs.

Using the DAC (digital-to-analogue converter)

The DAC on the PCF8591 has a resolution of 8-bits – so it can generate a theoretical signal of between zero volts and the reference voltage (Vref) in 255 steps. For demonstration purposes we’ll use a Vref of 5V, and you can use a lower Vref such as 3.3V or whatever you wish the maximum value to be … however it must be less than the supply voltage. Note that when there is a load on the analogue output (a real-world situation), the maximum output voltage will drop – the data sheet (which you downloaded) shows a 10% drop for a 10kΩ load. Now for our demonstration circuit:

pcf8591basic_schem

Note the use of 10kΩ pull-up resistors on the I2C bus, and the 10μF capacitor between 5V and GND. The I2C bus address is set by a combination of pins A0~A2, and with them all to GND the address is 0x90. The analogue output can be taken from pin 15 (and there’s a seperate analogue GND on pin 13. Also, connect pin 13 to GND, and circuit GND to Arduino GND.

To control the DAC we need to send two bytes of data. The first is the control byte, which simply activates the DAC and is 1000000 (or 0x40) and the next byte is the value between 0 and 255 (the output level). This is demonstrated in the following sketch:

Did you notice the bit shift of the bus address in the #define statement? Arduino sends 7-bit addresses but the PCF8591 wants an 8-bit, so we shift the byte over by one bit. 

The results of the sketch are shown below, we’ve connected the Vref to 5V and the oscilloscope probe and GND to the analogue output and GND respectively:

triangle

If you like curves you can generate sine waves with the sketch below. It uses a lookup table in an array which contains the necessary pre-calculated data points:

And the results:

sine

For the following DSO image dump, we changed the Vref to 3.3V – note the change in the maxima on the sine wave:

sine3v3

Now you can experiment with the DAC to make sound effects, signals or control other analogue circuits.

Using the ADCs (analogue-to-digital converters)

If you’ve used the analogRead() function on your Arduino (way back in Chapter One) then you’re already familiar with an ADC. With out PCF8591 we can read a voltage between zero and the Vref and it will return a value of between zero and 255 which is directly proportional to zero and the Vref. For example, measuring 3.3V should return 168. The resolution (8-bit) of the ADC is lower than the onboard Arduino (10-bit) however the PCF8591 can do something the Arduino’s ADC cannot. But we’ll get to that in a moment.

First, to simply read the values of each ADC pin we send a control byte to tell the PCF8591 which ADC we want to read. For ADCs zero to three the control byte is 0x00, 0x01, ox02 and 0x03 respectively. Then we ask for two bytes of data back from the ADC, and store the second byte for use. Why two bytes? The PCF8591 returns the previously measured value first – then the current byte. (See Figure 8 in the data sheet). Finally, if you’re not using all the ADC pins, connect the unused ones to GND.

The following example sketch simply retrieves values from each ADC pin one at a time, then displays them in the serial monitor:

Upon running the sketch you’ll be presented with the values of each ADC in the serial monitor. Although it was a simple demonstration to show you how to individually read each ADC, it is a cumbersome method of getting more than one byte at a time from a particular ADC.

To do this, change the control byte to request auto-increment, which is done by setting bit 2 of the control byte to 1. So to start from ADC0 we use a new control byte of binary 00000100 or hexadecimal 0x04. Then request five bytes of data (once again we ignore the first byte) which will cause the PCF8591 to return all values in one chain of bytes. This process is demonstrated in the following sketch:

Previously we mentioned that the PCF8591 can do something that the Arduino’s ADC cannot, and this is offer a differential ADC. As opposed to the Arduino’s single-ended (i.e. it returns the difference between the positive signal voltage and GND, the differential ADC accepts two signals (that don’t necessarily have to be referenced to ground), and returns the difference between the two signals. This can be convenient for measuring small changes in voltages for load cells and so on.

Setting up the PCF8591 for differential ADC is a simple matter of changing the control byte. If you turn to page seven of the data sheet, then consider the different types of analogue input programming. Previously we used mode ’00’ for four inputs, however you can select the others which are clearly illustrated, for example:

adcmodes

So to set the control byte for two differential inputs, use binary 00110000 or 0x30. Then it’s a simple matter of requesting the bytes of data and working with them. As you can see there’s also combination single/differential and a complex three-differential input. However we’ll leave them for the time being.

Conclusion

Hopefully you found this of interest, whether adding a DAC to your experiments or learning a bit more about ADCs. We’ll have some more analogue to digital articles coming up soon, so stay tuned. And if you enjoy my tutorials, or want to introduce someone else to the interesting world of Arduino – check out my new book “Arduino Workshop” from No Starch Press.

LEDborder

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The following two tabs change content below.

John Boxall

Founder, owner and managing editor of tronixstuff.com.

14 Responses to “Tutorial – Arduino and PCF8591 ADC DAC IC”

  1. Elac says:

    Nce tutoral,
    Would ths help smooth the voce output of the TTS lbrary?
    As t uses PWM for speech synthess.
    Thanks

  2. mike says:

    ADC part of the tutoral does not work, Seral montor screen shows ASCII ( I am guessng ) characters that does not make sense. Does anyone have the same knd of problem?
    thanks

    • John Boxall says:

      Check you have the speed set to 9600.

      • mike says:

        Baud rate s 9600. My problem s , when start rotatng a potentometer ( changng voltage on the An. nput ) am gettng somethng lke ths , n the SERIAL MONITOR :
        0123456789 abcdef….. ><?:"{P!!@#$%^&* … and so on.

        The above mentoned characters appear n the seral montor wndow one at a tme.
        My queston actually s f anyone worked on ths project and made t work?
        If t worked as descrbed, what am gong to expect to see n seral montor wndow. numbers? ASCII characters? Anythng else?

        Thank you n advance. Any help wll be apprecated.

      • John Boxall says:

        Example 52.3/4 dsplay the values for the nputs across one lne, then repeat.
        E.g.
        456 798 120 456
        869 451 5 125
        etc.

      • mike says:

        Thank you for your nput. I just made t work.
        I modfed your code slghtly for my project purposes and now t works perfect for me.
        Thank you

        #nclude “Wre.h”
        #nclude // nclude the lbrary code:
        #defne PCF8591 (0x90 >> 1) // I2C bus address
        #defne ADC0 0x00 // control bytes for readng ndvdual ADCs
        #defne ADC1 0x01
        #defne ADC2 0x02
        #defne ADC3 0x03
        byte value0, value1, value2, value3;
        LqudCrystal lcd(12, 11, 5, 4, 3, 2); // ntalze lb wth ntf pns
        nt real_volt ;
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        vod setup()
        {
        Wre.begn();
        Seral.begn(9600);
        lcd.begn(16, 2); // set up the LCD’s columns and rows:
        }
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        vod loop()
        {
        Wre.begnTransmsson(PCF8591); // wake up PCF8591
        Wre.send(ADC0); // control byte – read ADC0
        Wre.endTransmsson(); // end tranmsson
        Wre.requestFrom(PCF8591, 2);
        value0=Wre.receve();
        value0=Wre.receve();

        nt real_volt = 1024/value0;
        //++++++++++++++++++++++++++
        lcd.prnt (real_volt, DEC);
        delay (500);
        lcd.clear ();
        //++++++++++++++++++++++++++
        Wre.begnTransmsson(PCF8591); // wake up PCF8591
        Wre.send(ADC1); // control byte – read ADC1
        Wre.endTransmsson(); // end tranmsson
        Wre.requestFrom(PCF8591, 2);
        value1=Wre.receve();
        value1=Wre.receve();
        //++++++++++++++++++++++++++
        lcd.prnt (value1, DEC);
        delay (500);
        lcd.clear ();
        //++++++++++++++++++++++++++

        Wre.begnTransmsson(PCF8591); // wake up PCF8591
        Wre.send(ADC2); // control byte – read ADC2
        Wre.endTransmsson(); // end tranmsson
        Wre.requestFrom(PCF8591, 2);
        value2=Wre.receve();
        value2=Wre.receve();
        //++++++++++++++++++++++++++
        lcd.prnt (value2, DEC);
        delay (500);
        lcd.clear ();
        //++++++++++++++++++++++++++

        Wre.begnTransmsson(PCF8591); // wake up PCF8591
        Wre.send(ADC3); // control byte – read ADC3
        Wre.endTransmsson(); // end tranmsson
        Wre.requestFrom(PCF8591, 2);
        value3=Wre.receve();
        value3=Wre.receve();
        //++++++++++++++++++++++++++
        lcd.prnt (value3, DEC);
        delay (500);
        lcd.clear ();
        //++++++++++++++++++++++++++

        //++++++++++++++++++++++++++++++++++++++++++++
        //++++++++++++++++++++++++++++++++++++++++++++
        Seral.prnt(“Dec:”);
        Seral.prntln(value0, DEC);
        Seral.prnt(” Hex:”);
        Seral.prntln(value0, HEX);
        Seral.prnt(” BIN:”);
        Seral.prntln ( value0, BIN);
        delay (500);
        //+++++++++++++++++++++++++++++++++++++++++++++
        //+++++++++++++++++++++++++++++++++++++++++++++

        }

      • John Boxall says:

        Well done, glad to hear you have t workng.

  3. mike says:

    Ths s great tutoral and am just tryng to make t work.

  4. Mat says:

    H sr
    ‘m a newbe n pckng adc to dac,
    s there an adc-dac c that has a mnmal port but has many bts 12bts to 24 bts wll do, that converts +12V and -12V analog sgnal to the arduno nterface?

    hence that the resoluton of the analog read of the arduno s only lmted to 10bts and 0 to +5V analog sgnal only
    thanks

    • John Boxall says:

      Sorry but there’s a huge range of ADC/DACs on the market. Have a look through the onlne catalogues of places lke element14 or dgkey, and manufacturer webstes.

Trackbacks/Pingbacks


Leave a Reply

Subscribe via email

Receive notifications of new posts by email.

The Arduino Book

Arduino Workshop

Für unsere deutschen Freunde

Dla naszych polskich przyjaciół ...

Australian Electronics!

Buy and support Silicon Chip - Australia's only Electronics Magazine.

Use of our content…

%d bloggers like this: