Current and a knob on an ESP32-C3
An INA219 in series with an LED and an ADS1115 on a potentiometer, both on I2C: milliamps through a real shunt, and volts to sixteen bits. The whole build, an ESP32-C3 and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
IntermediateRuns in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A load's current and its voltage, two ways: an INA219 at 0x40 and an
// ADS1115 at 0x48 on the ESP32-C3's I2C pins, GPIO 8 and 9.
//
// The INA219 is in series with the load: 3.3 V into VIN+, VIN- on to a 220
// ohm resistor to ground. Its 0.1 ohm shunt is a real resistor in the
// circuit, and the chip reads the few millivolts across it to 10 uV, and the
// voltage on the load side to 4 mV. The library writes the chip's calibration
// in begin(); without it the current register reads zero whatever flows.
//
// The ADS1115's A0 is on the same load-side node, so the two chips measure
// the same voltage: the INA219 in 4 mV steps, the ADS1115 at GAIN_ONE in
// 125 uV steps, a single-shot conversion taking 7.8 ms at the default rate.
//
// Change the resistor in the properties panel and watch both follow.
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_ADS1X15.h>
Adafruit_INA219 ina219;
Adafruit_ADS1115 ads;
void setup() {
Serial.begin(115200);
if (!ina219.begin()) Serial.println("no INA219 at 0x40");
ads.setGain(GAIN_ONE);
if (!ads.begin()) Serial.println("no ADS1115 at 0x48");
}
void loop() {
int16_t counts = ads.readADC_SingleEnded(0);
Serial.print("load ");
Serial.print(ina219.getCurrent_mA());
Serial.print(" mA, ");
Serial.print(ina219.getPower_mW());
Serial.print(" mW INA219 says ");
Serial.print(ina219.getBusVoltage_V());
Serial.print(" V, ADS1115 says ");
Serial.print(ads.computeVolts(counts));
Serial.print(" V (");
Serial.print(counts);
Serial.println(" counts)");
delay(500);
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- ESP32-C3-DevKitM-1 pin 3V3; Sixteen-bit ADC, ADS1115 pin VDD; Current monitor, INA219 pin VCC; Current monitor, INA219 pin VIN+
- Ground: ESP32-C3-DevKitM-1 pin GND; Resistor, 220 Ω pin 1; Sixteen-bit ADC, ADS1115 pin GND; Current monitor, INA219 pin GND
- ESP32-C3-DevKitM-1 pin 8; Sixteen-bit ADC, ADS1115 pin SDA; Current monitor, INA219 pin SDA
- ESP32-C3-DevKitM-1 pin 9; Sixteen-bit ADC, ADS1115 pin SCL; Current monitor, INA219 pin SCL
- Resistor, 220 Ω pin 2; Sixteen-bit ADC, ADS1115 pin A0; Current monitor, INA219 pin VIN-
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.