LM311 zero-cross detector into an Arduino on an Arduino Uno
An LM311 on +12 V and -12 V watches a 5 V sine cross ground and pulls an Uno pin low on every negative half: an open collector, with the Uno as its pull-up. The whole build, an Arduino Uno and 2 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.
Intermediate Runs in your browser. Free, and no account needed.
The parts retail for about $30. 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.
// Button: a pushbutton on pin 2 lights the LED on pin 13.
//
// INPUT_PULLUP switches on the ATmega328P's 35 k pull-up, so the pin sits at
// 5 V until the button pulls it to GND. That is why the button reads LOW when
// it is pressed: wire one side to pin 2 and the other to GND, no resistor.
const int BUTTON_PIN = 2;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(5);
}
Parts list
5 parts, plus the jumper wires and a bench instrument. The parts retail for about $30; on Mokxi they're free, and so are the instruments.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Power, 12 V
- 1 × Power, -12 V
- 1 × Function generator
- 1 × LM311 comparator
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.
- Arduino Uno R3 pin 2; LM311 comparator pin COL
- Ground: Arduino Uno R3 pin GND; Function generator pin GND; LM311 comparator pin EMIT; LM311 comparator pin IN-
- 12 V: LM311 comparator pin VCC+
- -12 V: LM311 comparator pin VCC-
- Function generator pin OUT; LM311 comparator 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.