Lesson 33 Analog-Digital Converter

Share for us

Introduction

The PCF8591 is a single-chip, single-supply and low-power 8-bit CMOS data acquisition device with four analog inputs, one analog output and a serial I2C-bus interface. Three address pins A0, A1 and A2 are used for programming the hardware address, allowing the use of up to eight devices connected to the I2C-bus without additional hardware. Address, control and data to and from the device are transferred serially via the two-line bidirectional I2C-bus.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * AD/DA Converter PCF8591

– 5 * DuPont wires (M to F)

Experimental Principle

Addressing

Each PCF8591 device in an I2C-bus system is activated by sending a valid address to the device. The address consists of a fixed part and a programmable part. The programmable part must be set according to the address pins A0, A1 and A2. The address always has to be sent as the first byte after the start condition in the I2C-bus protocol. The last bit of the address byte is the read/write-bit which sets the direction of the following data transfer (see the figure below).

Control byte

The second byte sent to a PCF8591 device will be stored in its control register and is required to control the device function. The upper nibble of the control register is used for enabling the analog output, and for programming the analog inputs as single-ended or differential inputs. The lower nibble selects one of the analog input channels defined by the upper nibble (see Fig.5). If the auto-increment flag is set, the channel number is incremented automatically after each A/D conversion. See the figure below.

In this experiment, set the second byte sent to a PCF8591 device as high, so as to enable the analog output – make AOUT output an analog quantity which changes gradually. The indicator light D2 on the AD/DA converter PCF8591 gradually lights up and goes out alternately. The same happens to the LED attached to pin 13 of the SunFounder Uno board if it is hooked up with AOUT.

Experimental Procedures

Step 1: Build the circuit

The wiring between PCF8591 and SunFounder Uno board:

PCF8591SunFounder Uno
VCC5V
GNDGND
SDAA4
SCLA5
AOUT13

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno board

We can see the indicator light D2 on the AD/DA converter PCF8591 gradually lights up and goes out alternately. The same happens to the LED attached to pin 13 of the SunFounder Uno board

 Code

/***********************************************
name:Analog-Digital Converter -PCF8591
function:the indicator light D2 on the PCF8591 gradually lights up and goes out alternately.
The same happens to the LED attached to pin 13 of the SunFounder Uno board
***************************************************/
//Email:support@sunfounder.com
//website:www.sunfounder.com#include “Wire.h”
#define PCF8591 (0x90 >> 1) // I2C bus address
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println(sin(PI/2));

}
void loop()
{
  for (int i=0; i<256; i++)
  {
    Wire.beginTransmission(PCF8591); // wake up PCF8591
    Wire.write(0x40); // control byte – turn on DAC (binary 01000000),analog OUTPUT
    Wire.write(i); // value to send to DAC
    Wire.endTransmission(); // end tranmission
    delay(10*sin(i/256.0*90/180*PI));
    Serial.println(100*sin(i/256.0*90/180*PI));
}for (int i=255; i>=0; –i)
{
    Wire.beginTransmission(PCF8591); // wake up PCF8591
    Wire.write(0x40); // control byte – turn on DAC (binary 1000000)
    Wire.write(i); // value to send to DAC
    Wire.endTransmission(); // end tranmission
    delay(10*sin(i/256.0*90/180*PI));
    Serial.println(100*sin(i/256.0*90/180*PI));
  }
}