Hello friends, I hope you all are doing great. Today, I am going to share the ESP32 Smart Street Light with Fault Detection - Proteus Simulation, project 109 of our IoT Projects series. Street lights burn a lot of energy for empty roads, and a broken lamp can stay dark for weeks before anyone reports it. A smart street light fixes both: it switches on at dusk, stays dim while nobody is around, brightens where someone walks, and reports a lamp that has failed. It is one of the most searched student projects, and here is a complete one with the circuit, the ESP32 code and a Proteus simulation in which a whole night passes.

The street has three LED lamps, real 1 W power LEDs driven by IRLZ44N MOSFETs from the ESP32's PWM pins. An LDR module tells the ESP32 when it gets dark, and a PIR motion sensor at each lamp tells it where someone is moving. At night every lamp glows at 20 %; where someone moves, that lamp and its neighbours go to 100 % for ten seconds. A 1 ohm resistor under each lamp turns its current into a voltage the ESP32 measures, so a lamp that is switched on but draws no current is reported as FAULTY, on the serial monitor and on a web dashboard. In this tutorial, we will go through the circuit, the LDR, the PIRs, the MOSFET drivers, the current measurement and the code. Then we run a night with passers-by and a broken lamp in the simulation. At the end, you can download the project with the sketch and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the ESP32 smart street light with fault detection in Proteus simulation:

Figure: The smart street light in a Proteus simulation.
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Smart Street Light Do?

  • Dusk and dawn: an LDR switches the lamps on when it gets dark and off at daybreak, with hysteresis.
  • Dimming: at night every lamp glows at 20 % to save energy.
  • Motion: a PIR at each lamp brings that lamp and its neighbours to 100 % for 10 s.
  • Fault detection: each lamp's current is measured; a lamp switched on but drawing nothing is FAULTY.
  • Energy: the energy used against three lamps at full power all night.
  • A dashboard: AUTO, ALL ON and ALL OFF from a laptop or a phone.

In the simulation, the LDR's pop-up sets the light, each PIR's pop-up walks a person in, each power LED's pop-up shows its current and light, and a slide switch cuts lamp 3's drive wire to test the fault detection.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.

The parts of the smart street light and their library posts
PartProteus deviceLibrary postConnection
ESP32 DevKit V1ESP32 DEVKIT V1ESP32 DevKit V1 Library for ProteusU1, ESP32_Street_Light.hex, Wi-Fi 192.168.1.101
LDR moduleLDRADVTEPLDR Sensor Library for ProteusU2: A0 on D34, VCC +3.3V
HC-SR501 PIR (x 3)HCSR501ADVTEPPIR Sensor Library for ProteusU3 - U5: OUT on D25, D26, D27
1 W power LEDs (x 3)POWERLED1WWHITETEPPower LED Library for ProteusD1 - D3: the lamps
IRLZ44N MOSFETs (x 3)IRLZ44NTEPN-MOSFET Library for ProteusQ1 - Q3: gates on D18, D19, D21
Power resistors (x 6)RESISTORPOWER5WTEPPower Resistor Library for ProteusR1 - R3 4.7 ohm, R4 - R6 1 ohm sense
Slide switchSWITCHSLIDETEPSlide Switch Library for ProteusSW1: lamp 3's drive wire
10k resistorRESISTOR4BANDTEPResistor Library for ProteusR7: Q3's gate pull-down
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Street Light: the Circuit

Figure: The complete circuit of the smart street light on an A3 Proteus sheet: one column a lamp.
The connections of each street lamp
LampPIRGate (PWM)Current (ADC)
Lamp 1 (D1)D25D18D32 (R4)
Lamp 2 (D2)D26D19D33 (R5)
Lamp 3 (D3)D27D21 (through SW1)D35 (R6)

Each lamp is a column on the sheet: the PIR on top, then the 4.7 ohm current-limit resistor from +5 V, the power LED, the MOSFET and the 1 ohm sense resistor to ground. The LED and its resistor set about 320 mA, near the 1 W LED's 350 mA rating. The LDR runs from 3.3 V so its output never exceeds what the ESP32's ADC can read.

How the Smart Street Light Works

Dusk and Dawn from the LDR

The LDR module is a light-dependent resistor in a divider: in the dark its resistance rises and so does the voltage on A0. The ESP32 reads it on D34. At night means above 2.0 V; day comes back only below 1.5 V. The gap between the two thresholds is hysteresis: at dusk the light changes slowly and the reading wobbles, and without the gap the lamps would flicker on and off for minutes.

Motion from the PIR Sensors

An HC-SR501 PIR sees the infrared of a warm body moving across its Fresnel lens and holds its output high for a few seconds after the last movement. There is one at each lamp. When a PIR sees someone, the program brings that lamp and its two neighbours to full brightness, so the light runs ahead of a pedestrian, and keeps them there for 10 s after the last movement. Then they fade back to the night's 20 %.

Driving a Power LED with a MOSFET

An ESP32 pin gives a few milliamps; a 1 W LED needs 350. The IRLZ44N is a logic-level N-MOSFET: a gate voltage of 3.3 V switches it fully on, so the pin can drive it directly. The LED sits between +5 V and the MOSFET's drain, with a 4.7 ohm power resistor to set the current. Dimming is a 500 Hz PWM on the gate: at 20 % the LED is on for 0.4 ms of every 2 ms, too fast for the eye to see, and it uses a fifth of the power.

Measuring the Current to Find a Faulty Lamp

The 1 ohm resistor between the MOSFET's source and ground carries the lamp's current, so its voltage is the current: 0.32 V for 320 mA. The ESP32 measures it on an ADC pin. Because the lamp is pulsed by the PWM, a single reading could fall in the off-time, so the program takes 32 readings spread over more than one 2 ms period and keeps the highest. A lamp that is switched on but reads less than 50 mA for 3 s in a row is FAULTY: the LED has failed open, a wire is broken or the driver is dead. When the current comes back, it is working again.

Energy Saving

From each lamp's duty and its rated power, the program adds up the energy used, and compares it with three lamps at full power all night. With the lamps at 20 % except where people walk, the saving is around 80 % on a quiet street, which is why cities are replacing their lamps with smart ones.

Why Measure the Current and Not the Light?

Many student projects detect a broken lamp with a second LDR pointed at it. That works on a bench, but on a street the second LDR sees the moon, the headlights of passing cars and its neighbour lamp, and it gets dirty. The current tells the truth about the lamp itself: an LED that glows draws current, an LED that has failed open, a broken wire or a dead driver draws none. It costs one resistor and one ADC pin a lamp, and the same reading also shows a lamp that draws too much, an early sign of a failing driver. Real street lighting controllers work the same way, through the LED driver's current or its fault output.

The Arduino Code Explained

The sketch is ESP32_Street_Light.ino, built for our ESP32 DevKit V1 with the WiFi and WebServer libraries. The pins and the settings:

const byte LDR = 34, PIR[3] = {25, 26, 27}, GATE[3] = {18, 19, 21}, SENSE[3] = {32, 33, 35};
const byte DIM = 20, FULL = 100;                                  // % at night, % with someone near
const float NIGHT_V = 2.0, DAY_V = 1.5, FAULT_A = 0.05, RATED_W = 1.0;   // LDR volts; the fault threshold; a lamp's rated power

Each gate gets a 500 Hz, 10-bit PWM, and a lamp's level becomes a duty:

  for (byte k = 0; k < 3; k++) { pinMode(PIR[k], INPUT); ledcAttach(GATE[k], 500, 10); ledcWrite(GATE[k], 0); }
  ledcWrite(GATE[k], (uint32_t)pct * 1023 / 100);

A lamp and its neighbours light up for someone near:

      for (int j = (int)k - 1; j <= (int)k + 1; j++)            // the lamp and its neighbours light up for someone near
        if (j >= 0 && j < 3 && lastMotion[j] && now - lastMotion[j] < HOLD_MS) pct = FULL;

The current: the highest of 32 readings spread over a PWM period, 1 V on the 1 ohm resistor being 1 A:

    delayMicroseconds(60);                                        // (spread over > 2 ms: a dim lamp is on only 0.4 ms of each 2 ms)
  return best / 1000.0;                                           // 1 ohm: 1 V = 1 A

A lamp on but drawing less than 50 mA for 15 checks in a row (3 s) is FAULTY, and the energy adds up:

    if (level[k] && amps[k] < FAULT_A) { if (lowCount[k] < 15) lowCount[k]++; } else lowCount[k] = 0;
    energyWh += RATED_W * level[k] / 100.0 * 0.2 / 3600.0;       // (the rated power scaled by the duty)

The dashboard's links are Refresh, AUTO, ALL ON and ALL OFF; /data gives every lamp's level, current, faults and motions with the energy as JSON.

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files (the steps are under the download button).
  2. Open ESP32-Street-Light.pdsprj from the Proteus Simulation folder. Keep the HEX and the DLL files next to it.
  3. Press Play: by day the lamps stay off. Click the LDR (U2) and press Night: the lamps glow at 20 %.
  4. Click a PIR (U3 - U5) and press Walk in: its lamp and the neighbours go to full brightness.
  5. Click SW1 and press Flip: lamp 3's drive wire is cut and 3 s later it is FAULTY. Open the Laptop or the Mobile WiFi Monitor (press Go) for the dashboard.

Simulation Results: a Night on the Street

We pressed Play by day, made it night with the LDR's pop-up, walked a person past lamp 1 and then lamp 3, cut lamp 3's drive wire with SW1 and mended it, and let the day come back. The power LEDs' pop-ups show each lamp's current and light. The times are the sketch's clock; the whole run took 23.0 simulated seconds.

By Day

The sketch printed READY - day: the lamps off. The LDR's pop-up showed indoor daylight, 300 lux on the sensor and 0.43 V on A0, far below the 2.0 V night threshold, so all three lamps stayed off.

Figure: By day: the lamps off.
Figure: The LDR by day.

Night Falls

We pressed Night on the LDR's pop-up: 0.2 lux, and A0 rose to 3.0 V. At 6.2 s: NIGHT - the LDR at 3.00 V: the lamps on at 20 %. Lamp 1's pop-up showed the 500 Hz PWM, about 20 % on, 63.7 mA on average and 18 lm, a fifth of its full light. In the PWM's on-time the sketch measured 0.319 A on each sense resistor. The dashboard showed all three lamps at 20 % and 80 % saved against full power.

Figure: Night: the lamps at 20 %.
Figure: The LDR at night.
Figure: Lamp 1 at 20 %.
Figure: The dashboard at night.

Someone Walks By

We walked a person into lamp 1's PIR. At 8.8 s: MOTION at lamp 1, and lamps 1 and 2, the lamp and its neighbour, went to 100 %: 322 mA and 90 lm on lamp 1's pop-up, with its junction warming to 34 C. At 11.0 s a person at lamp 3: MOTION at lamp 3, and now all three lamps were at full brightness, lamp 2 being the neighbour of both. The saving dropped to 40 % while people were about.

Figure: Motion at lamp 1.
Figure: The PIR at lamp 1.
Figure: Lamp 1 at full brightness.
Figure: Motion at lamp 3: all lamps full.
Figure: The dashboard: full brightness.

A Faulty Lamp

Then we flipped SW1, cutting lamp 3's drive wire; R7 held its gate low and the MOSFET switched off. Lamp 3 was supposed to be at 100 %, but its pop-up showed the LED dark with a current of 10 picoamps. Three seconds of near-zero readings later, at 16.0 s: LAMP 3 FAULTY - on at 100 % but 0.000 A. The dashboard on the laptop and the phone marked lamp 3 FAULTY, while lamps 1 and 2 kept their 0.323 A. We flipped SW1 back, and at 17.8 s: LAMP 3 working again - 0.319 A.

Figure: Lamp 3 dark: FAULTY.
Figure: SW1 cuts the drive wire.
Figure: Lamp 3: no current.
Figure: The dashboard: LAMP 3 FAULTY.
Figure: The phone: the fault reported.

Daybreak

Finally the LDR's Indoor light, daylight on the sensor, brought A0 back to 0.43 V, below the 1.5 V day threshold. At 18.8 s: DAY - the LDR at 0.43 V: the lamps off. The serial monitor kept the whole night.

Figure: Daybreak: the lamps off.
Figure: The serial monitor.

Download the Project Files

Click the button below to download ESP32-Smart-Street-Light-Fault-Detection-Proteus-Simulation-v1.0.zip (about 5.2 MB, without the source code of the models):

ESP32 Smart Street Light with Fault Detection - Proteus Simulation V1.0
  • Proteus Simulation: ESP32-Street-Light.pdsprj, ESP32_Street_Light.hex and copies of the models.
  • Arduino Code: ESP32_Street_Light.ino, the sketch.
  • Proteus Library Files: the LIB files of every part.
  • Proteus Model Files: the DLL models.
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy the files from Proteus Model Files into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

Open the sketch in the Arduino IDE with the TEP Boards for Proteus installed, select ESP32 Dev Module (TEP Proteus), and use Sketch > Export Compiled Binary. Put the new HEX next to the project. The dim level, the hold time, the LDR thresholds and the fault threshold are at the top of the sketch.

Ideas to Extend the Project

  • Send the fault reports to the city's server or to a phone, with the lamp's location.
  • Add a real-time clock: dimmer after midnight, off on moonlit summer nights.
  • Count the people passing each lamp and map the busy streets.
  • Measure the LED's temperature and lower the current on hot nights.
  • Link the lamps by LoRa or Zigbee so one controller watches a whole street.
  • Add a radar sensor for cars, which a PIR sees poorly through a windscreen.

From Simulation to Real Hardware

  • Real lamps: a street lamp is a mains LED driver; switch it with a relay or use a dimmable driver with a 0-10 V or PWM input.
  • Current sensing: on mains lamps use a current sensor (ACS712, INA219 or a current transformer) or the driver's fault output.
  • The gates: add a 100 ohm resistor in series with each gate and a 10 k pull-down, as R7 here, so the MOSFETs stay off at boot.
  • The ESP32's ADC: it is not very accurate near 0 V; the 50 mA threshold leaves plenty of margin.
  • Heat: a 1 W LED needs a small heatsink on a real board; its pop-up in the simulation shows where its junction is heading.

Troubleshooting

  • The lamps come on by day: the LDR's A0 is above 2.0 V; more light on it, or raise NIGHT_V.
  • A lamp is FAULTY but glows: check its 1 ohm sense resistor and its ADC pin.
  • The lamps flicker at dusk: keep the two LDR thresholds apart; that is the hysteresis.
  • No dashboard: the ESP32 joins TEP-WiFi a few seconds after the start; press Go again.

Limitations of the Simulation

  • The LDR, the PIRs, the power LEDs, the MOSFETs and the resistors are our libraries' models, built from their datasheets and the parts' behaviour, not every detail of real parts.
  • The light and the passers-by are set in the pop-ups: the LDR's light level and each PIR's person.
  • The fault is a cut gate wire (SW1), not a burnt-out LED; for the current measurement it is the same: a lamp switched on that draws nothing. The slide switch is rated for 300 mA, too little for the lamp's own wire.
  • ESP32 is a product of Espressif Systems; this project is not made or endorsed by Espressif or by Labcenter.

So, that was all about the ESP32 Smart Street Light with Fault Detection - Proteus Simulation. I hope it helps you understand how an LDR and PIR sensors decide when and where to light a street, how an ESP32 drives power LEDs through MOSFETs, and how measuring each lamp's current finds a broken lamp by itself, all tested in a simulation before you build it. More popular projects with the ESP32 and the Raspberry Pi 5 are coming. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!