Projects / ADC and DAC lab on one chip

ADC and DAC lab on one chip

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 9 done
  1. NANO

    Wire the I2C lines

    A4 and A5 to the chip and the display.

  2. NANO

    Wire power

    5V and GND to both.

  3. NANO

    Wire the pots

    Outer legs to 5V and GND, wipers to AIN0 and AIN1.

  4. NANO846 lux

    Wire the light sensor

    LDR and 10 kilohm divider midpoint to AIN2.

  5. NANO

    Wire the loop

    AOUT to AIN3.

  6. NANO

    Upload the sketch

    Five numbers appear on the display.

  7. NANO

    Turn each pot

    AIN0 and AIN1 follow from 0 to 255.

  8. NANO849 lux

    Cover the LDR

    AIN2 changes.

  9. NANOPCF8591

    Watch the loop

    AIN3 tracks the DAC ramp.

In short

How do you build an ADC and DAC lab on one chip?

A PCF8591 gives four analog inputs and one analog output over I2C, for simple lab experiments. You need Arduino Nano, PCF8591 ADC/DAC, Potentiometer 10 kΩ × 2, LDR Photoresistor, Resistor 10 kΩ, SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 9 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Nano A4 (SDA)PCF8591 SDA and OLED SDAShared I2C data
Nano A5 (SCL)PCF8591 SCL and OLED SCLShared I2C clock
Nano 5VPCF8591 VCC, OLED VCC, pot endsPower
Nano GNDPCF8591 GND, OLED GND, pot endsGround
Pot 1 wiperPCF8591 AIN0Input 0
Pot 2 wiperPCF8591 AIN1Input 1
LDR and 10 kilohm divider midpointPCF8591 AIN2Input 2
PCF8591 AOUTPCF8591 AIN3Loop the output back into an input

Code

The PCF8591 is a small eight-bit converter with four analog inputs and one analog output, all controlled over I2C at address 0x48. To read an input you write a control byte that selects the channel, then read two bytes back: the first is the result of the previous conversion, so the code throws it away. The output is written by sending a control byte with bit 6 set and then the value from 0 to 255, which becomes a voltage from 0 to the supply. Connecting AOUT to AIN3 closes a loop so you can compare what you wrote with what you read back and see the small errors of an eight-bit converter: each step is about 20 millivolts at 5 V. The potentiometers and the light sensor give three more signals to practise on. Many ready-made boards already carry a pot, an LDR and a thermistor on the first three inputs through jumpers; if you use one, remove those jumpers so your own wiring is the only signal. The converter is slow compared with a microcontroller input, but it is a neat way to add channels and an output to a board.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

const uint8_t PCF = 0x48;                 // default address, A0 to A2 low
Adafruit_SSD1306 oled(128, 64, &Wire, -1);

uint8_t dacValue = 0;
int8_t dacStep = 4;

// Read one input channel (0 to 3). The first byte returned is the previous
// conversion, so read two bytes and use the second.
uint8_t readChannel(uint8_t ch) {
  Wire.beginTransmission(PCF);
  Wire.write(0x40 | (ch & 3));            // 0x40 keeps the analog output enabled
  Wire.endTransmission();
  Wire.requestFrom(PCF, (uint8_t)2);
  Wire.read();                            // stale value, discard
  return Wire.read();
}

void writeDac(uint8_t v) {
  Wire.beginTransmission(PCF);
  Wire.write(0x40);                       // control byte, output enabled
  Wire.write(v);
  Wire.endTransmission();
}

void setup() {
  Wire.begin();
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.setTextSize(1);
  oled.setTextColor(SSD1306_WHITE);
}

void loop() {
  dacValue += dacStep;                    // triangle ramp on the output
  if (dacValue >= 252 || dacValue <= 4) dacStep = -dacStep;
  writeDac(dacValue);
  delay(5);

  uint8_t a0 = readChannel(0), a1 = readChannel(1), a2 = readChannel(2), a3 = readChannel(3);

  oled.clearDisplay();
  oled.setCursor(0, 0);  oled.print("AIN0 pot1 "); oled.println(a0);
  oled.print("AIN1 pot2 ");  oled.println(a1);
  oled.print("AIN2 LDR  ");  oled.println(a2);
  oled.print("AIN3 loop ");  oled.println(a3);
  oled.print("DAC out   ");  oled.println(dacValue);
  oled.display();
  delay(40);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Pot 1 fully downAIN0 near 0
Pot 1 fully upAIN0 near 255
LDR coveredAIN2 changes
DAC at mid rampAIN3 close to the DAC value
Disconnect SDADisplay freezes or the values stop

If it does not work

Display blank

Check the OLED address is 0x3C and the wiring.

All values 0 or 255

Check the PCF8591 power and the AIN jumper settings on the board.

AIN3 not following

Check the AOUT to AIN3 wire.

Values jumpy

Add a small capacitor from each input to GND.

Chip not found

Scan the I2C bus and check the address pins.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ADC and DAC lab on one chip?

Arduino Nano, PCF8591 ADC/DAC, Potentiometer 10 kΩ × 2, LDR Photoresistor, Resistor 10 kΩ, SSD1306 OLED Display and Breadboard 830 Tie Points.

How long does the ADC and DAC lab on one chip take to build?

About 90 minutes for a beginner maker, in 9 steps.

How do I know the ADC and DAC lab on one chip works?

Pot 1 fully down: AIN0 near 0. Pot 1 fully up: AIN0 near 255. LDR covered: AIN2 changes.

Display blank: what should I check?

Check the OLED address is 0x3C and the wiring.

All values 0 or 255: what should I check?

Check the PCF8591 power and the AIN jumper settings on the board.

AIN3 not following: what should I check?

Check the AOUT to AIN3 wire.

Values jumpy: what should I check?

Add a small capacitor from each input to GND.