Simulator

An LDR simulator: a photoresistor night light on an Arduino

Mokxi is a free online LDR (photoresistor) simulator that runs in your browser, with nothing to install and no account needed. A light dependent resistor and a 10 k resistor as a divider on A0 of an Arduino Uno, and a lamp that fades up when it gets dark. Drag the light level down and watch the voltage fall and the lamp come on.

  • 505parts on the bench
  • 45boards running now
  • 1.00xreal time, on every board
Arduino Uno: an LDR night lightlive0.000 s 0.00x
Click to open it in the editor
Click the circuit to open it in the editor, then drag the LDR’s light slider down toward dark. The serial monitor prints the reading and the lamp’s brightness.

What you can do here

In the circuit above, or in the editor it opens.

  • Set the light on the LDR in lux, from darkness to daylight
  • Read the divider’s voltage on A0 with analogRead
  • Fade a lamp on at dusk with PWM
  • See two thresholds keep the lamp from flickering at twilight
  • Swap the 10 k resistor and see how the useful range moves
  • Measure the LDR’s voltage with a multimeter

What the running circuit shows

Light in, voltage out.

An LDR’s resistance falls as light hits it, and a pin cannot read resistance, so the LDR goes in a divider with a fixed 10 k resistor and the pin reads the voltage between them. In room light, 300 lux, A0 sits at 4.18 V and the sketch prints “light (856): lamp at 0”. At 100 lux it is 3.63 V, at 50 lux 3.18 V, at 10 lux 2.00 V, and at 1 lux 0.72 V, where the line reads “dark (147): lamp at 255”.

Notice how uneven that is: going from 300 to 1000 lux moves the pin by less than 0.4 V, while going from 10 lux to 1 moves it by 1.3 V. An LDR is far more sensitive in the dark end, which suits a night light well.

The sketch calls it dark under a reading of 380 and light over 580, and between the two it keeps whatever it last decided. So at 10 lux, a reading in between, the lamp stays as it was instead of flickering. When it does switch, it fades over about 0.6 seconds rather than snapping.

How the LDR is modeled

The curve of a common part.

The model follows the resistance curve of a GL5528, the LDR in most kits: around 10 k at 10 lux, far higher in the dark and a few hundred ohms in bright light. That is why 10 lux reads exactly half the supply against the 10 k resistor.

What it leaves out is time. A real LDR takes tens of milliseconds to respond to more light and can take a second or more to settle when the light goes away; this one follows the slider at once. Real LDRs also vary a good deal from one to the next, so choose thresholds from readings taken with the part you have.

How it works

Three steps from a blank tab to a running circuit.

  1. Open the editor

    It runs in your browser tab. Nothing to install, and you need no account to start.

    Your first project
  2. Wire it and write the code

    Drag parts onto a breadboard, run each wire from pin to pin, and write the sketch beside it.

    Wiring
  3. Press Run

    Your sketch compiles in the tab and runs on a model of the chip, with the Serial Monitor beside it.

    Run and serial
Reviews

What makers say about Mokxi.

“The AI checker helps check and correct my circuits, and it has way more components than Tinkercad. As a student I really appreciate the tool.”

ChloeEngineering student

“I’d been looking for a real online electronic simulator that was intuitive and complete when I found Mokxi. I was able to quickly import a Fritzing file and my Arduino sketch and start testing. Mokxi has all of the makings for the best online Arduino simulator I have been able to find!”

JayModel builderFeatured in his DioramaOS post

“I’m using Mokxi to develop and simulate an STM32-based four-wheel-drive robot car before building the actual hardware. It allows me to experiment with DC motor control, PWM, RPM measurement using photointerrupters, and different acceleration, braking, and reversing strategies, helping me understand and refine the system before implementing it in real life.”

NusretRobot car builder, Hamburg

Share your story

FAQ

Questions, answered.

Is it free?

Yes. The circuit on this page, every part in it and the editor it opens in are on the free plan, with no card.

Do I need an account?

No. The circuit runs the moment the page does, and the editor opens without one. A free account is only for saving a project to the cloud and sharing it by link.

Is an LDR the same as a photoresistor?

Yes. LDR stands for light dependent resistor; photoresistor and photocell are other names for the same part.

What resistor should I use with an LDR?

10 k is the usual choice and the one in this circuit. It puts the middle of the pin’s range near 10 lux, dim indoor light. Use a smaller resistor to tell bright levels apart, a larger one for very dark ones.

Why does my LDR light flicker at dusk?

With one threshold, a reading that hovers at it switches the lamp on and off. Use two, as this sketch does: dark under 380, light over 580, and no change in between.

More simulators

Each one opens on a circuit that is already running.

Turn the lights down

Open the circuit, drag the light level down and watch the lamp fade on. Nothing to install, and no account needed.