Opto press on an Arduino Uno
The Uno presses another gadget’s button through a 4N35 optocoupler every two seconds, and the two circuits never share a wire. The whole build, an Arduino Uno and 5 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 code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Opto press: the Uno "presses" a button on another gadget through a 4N35
// optocoupler, and the two circuits never share a wire.
//
// 4N35, LED side A (pin 1) -> pin 2 through 220 ohm K (pin 2) -> GND
// 4N35, other side C (pin 5) and E (pin 4) across the gadget's button
// the gadget its own two AA cells, 330 ohm and a red LED, which
// light whenever its button is pressed
//
// Inside the 4N35 an infrared LED shines on a phototransistor. Put current
// through the LED and the transistor conducts between C and E, which is all a
// button does. The Arduino Starter Kit uses this to press the buttons of a
// toy or a remote it cannot be wired to: the gadget's circuit and the board's
// circuit have different supplies and no common ground, and nothing that
// happens on one side can reach the other.
//
// Every two seconds the sketch presses for 300 ms. Watch the gadget's LED.
// Its two cells are not connected to the Uno at all.
const int OPTO_PIN = 2;
const unsigned long PERIOD_MS = 2000;
const unsigned long PRESS_MS = 300;
void setup() {
Serial.begin(9600);
pinMode(OPTO_PIN, OUTPUT);
Serial.println("Opto press: pressing every 2 s");
}
void loop() {
digitalWrite(OPTO_PIN, HIGH); // LED on: the transistor closes the button
Serial.println("press");
delay(PRESS_MS);
digitalWrite(OPTO_PIN, LOW);
Serial.println("release");
delay(PERIOD_MS - PRESS_MS);
}
Parts list
7 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
7 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; Resistor, 220 Ω pin 1
- Ground: Arduino Uno R3 pin GND; Optocoupler, 4N35 pin K
- Optocoupler, 4N35 pin A; Resistor, 220 Ω pin 2
- Optocoupler, 4N35 pin E; Battery, 9 V, AA or 18650 pin V-
- Optocoupler, 4N35 pin C; LED, red pin C
- Resistor, 330 Ω pin 1; Battery, 9 V, AA or 18650 pin V+
- Resistor, 330 Ω pin 2; LED, red pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.