Build it step by step
0 of 9 doneWire the I2C lines
A4 and A5 to the chip and the display.
Wire power
5V and GND to both.
Wire the pots
Outer legs to 5V and GND, wipers to AIN0 and AIN1.
Wire the light sensor
LDR and 10 kilohm divider midpoint to AIN2.
Wire the loop
AOUT to AIN3.
Upload the sketch
Five numbers appear on the display.
Turn each pot
AIN0 and AIN1 follow from 0 to 255.
Cover the LDR
AIN2 changes.
Watch the loop
AIN3 tracks the DAC ramp.
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
- Development BoardsBuyArduino Nano × 1
- Integrated CircuitsBuyPCF8591 ADC/DAC × 1
- PassivesBuy
- SensorsBuy
- PassivesBuyResistor 10 kΩ × 1
- DisplaysBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Nano A4 (SDA) | PCF8591 SDA and OLED SDA | Shared I2C data |
| Nano A5 (SCL) | PCF8591 SCL and OLED SCL | Shared I2C clock |
| Nano 5V | PCF8591 VCC, OLED VCC, pot ends | Power |
| Nano GND | PCF8591 GND, OLED GND, pot ends | Ground |
| Pot 1 wiper | PCF8591 AIN0 | Input 0 |
| Pot 2 wiper | PCF8591 AIN1 | Input 1 |
| LDR and 10 kilohm divider midpoint | PCF8591 AIN2 | Input 2 |
| PCF8591 AOUT | PCF8591 AIN3 | Loop 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.
| Check | Expected |
|---|---|
| Pot 1 fully down | AIN0 near 0 |
| Pot 1 fully up | AIN0 near 255 |
| LDR covered | AIN2 changes |
| DAC at mid ramp | AIN3 close to the DAC value |
| Disconnect SDA | Display 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 →
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.
