Built-in project · Arduino Uno R3

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 About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $34. On Mokxi they're free.

Opto press · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Optocouplers and electrical isolation
  • Two circuits with no common ground
  • A phototransistor acting as a switch
  • Limiting LED current with a resistor
  • Timing a press and release
  • Difficulty: Intermediate
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 7

How it works

A 4N35 optocoupler has an infrared LED and a phototransistor inside, with no electrical link between them. The Uno's pin 2 drives the LED side through a 220 ohm resistor.

The other side stands in for a gadget's button. It has its own battery, a 330 ohm resistor and a red LED, with no shared wire to the Uno. When the infrared LED shines, the transistor conducts and the gadget's LED lights, as if its button were pressed.

The sketch presses for 300 ms, then waits, so a press happens every 2 seconds. It prints "press" and "release" to serial.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω pin 1Arduino Uno R3 pin 2
Optocoupler, 4N35 pin KArduino Uno R3 pin GND
Resistor, 220 Ω pin 2Optocoupler, 4N35 pin A
Battery: AA, coin cell, LiPo pin V-Optocoupler, 4N35 pin E
LED, red pin COptocoupler, 4N35 pin C
Battery: AA, coin cell, LiPo pin V+Resistor, 330 Ω pin 1
LED, red pin AResistor, 330 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// 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);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

It opens serial at 9600 and makes OPTO_PIN (pin 2) an output. Then it prints that it is pressing every 2 seconds.

void setup() {
  Serial.begin(9600);
  pinMode(OPTO_PIN, OUTPUT);
  Serial.println("Opto press: pressing every 2 s");
}

loop sketch.ino

It sets the pin HIGH, which lights the optocoupler's LED and closes the other side. It prints "press" and waits PRESS_MS (300 ms). Then it sets LOW, prints "release" and waits the rest of PERIOD_MS (2000 ms) so the full cycle lasts two seconds.

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);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The gadget's LED never lights This project
    Check the 4N35 pins: A to the 220 ohm resistor, K to GND, and C and E across the gadget's side. Also check the battery connections.
  • You joined the two grounds This project
    Keep them separate. The Uno's GND goes only to K, and the battery's minus goes only to E, so the sides stay isolated.
  • The output side never switches. The input LED needs a series resistor, about 330 ohms to 1k from 5 V, and enough current, about 10 mA, to drive the transistor. Optocoupler
    Put a resistor in series with the LED, and a pull-up resistor on the collector side. A 4N35 has a modest current transfer ratio, so give the LED enough current.
  • The two sides share a ground, which defeats the isolation, and the base pin is left connected wrongly. Optocoupler
    Keep the two circuits on separate grounds. Leave the base pin (6) unconnected, or add a resistor from base to emitter to speed it up. Check the dot for pin 1.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.