Arduino on 12 V with an LM2596, on an Arduino Uno
A 12 V adapter into an LM2596 buck module set to 5 V, its output on the Uno 5V pin, and A0 reading the adapter through a 10k and 4.7k divider. The whole build, an Arduino Uno and 4 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.
Teaching? Assign it to your class in one click.
Beginner Runs in your browser. Free, and no account needed.
The parts retail for about $36. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// An Arduino on a 12 V adapter, through an LM2596 buck converter.
//
// 12 V the adapter's jack into the LM2596 module's IN+ and IN-
// 5V the module's OUT+ into the Uno's 5V pin, OUT- to GND
// A0 the adapter's 12 V through 10k, with 4.7k from A0 to GND
//
// SET THE TRIMMER FIRST. A module comes set to whatever the factory left it
// at, often well over 5 V. On the bench, turn the brass screw with a meter on
// OUT+ before the Uno is on it: the 5V pin goes straight to the chip, with no
// regulator to save it. Here the module's volts property is the trimmer.
//
// WHY A BUCK. A 7805 would do the same job by burning the difference: at
// 200 mA it takes 200 mA from the adapter and turns (12 - 5) x 0.2 = 1.4 W
// into heat. The LM2596 switches 150,000 times a second and trades volts for
// amps instead: 5 V at 200 mA is 1 W, and at about 85 % that is under 100 mA
// from 12 V.
//
// THE DIVIDER. A0 reads 0 to 5 V, and 12 V would wreck it, so 10k and 4.7k
// bring it down to 4.7 / 14.7 of the adapter: 3.84 V, which reads 785 of 1023.
// The sketch undoes the divider and prints the adapter's volts.
//
// THINGS TO TRY. Swap the adapter for 9 V and watch the reading follow it
// while 5V stays put. Swap it for 5 V: the LM2596 needs about a volt across
// itself, and the Mokxi Uno, which also has its USB power, holds its own 5V.
//
// Press Run.
const int SENSE_PIN = A0;
const float DIVIDER = (10000.0 + 4700.0) / 4700.0; // the 10k and 4.7k
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println("Arduino on 12 V through an LM2596");
}
void loop() {
int raw = analogRead(SENSE_PIN); // 0 to 1023 for 0 to 5 V
float pin = raw * 5.0 / 1023.0;
float adapter = pin * DIVIDER;
Serial.print("A0 ");
Serial.print(raw);
Serial.print(", adapter ");
Serial.print(adapter, 1);
Serial.println(" V");
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
Parts list
6 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.
How it is wired
6 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.
- Ground: Arduino Uno R3 pin GND; LM2596 buck converter module pin OUT-
- Arduino Uno R3 pin 5V; LM2596 buck converter module pin OUT+
- Ground: Arduino Uno R3 pin GND; Resistor, 4.7k Ω pin 2
- Arduino Uno R3 pin A0; Resistor, 10k Ω pin 2; Resistor, 4.7k Ω pin 1
- DC jack and adapter pin V+; LM2596 buck converter module pin IN+; Resistor, 10k Ω pin 1
- DC jack and adapter pin V-; LM2596 buck converter module pin IN-
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.