Fareed Mohmed← Project library

Engineering · Raspberry Pi · Project 44 of 47 · Smart farm and energy · Beginner

Sunshine meter with an LED bar

One LDR measures the daylight. The LED bar shows how sunny it is, from one LED (dark) to five (bright). Useful for choosing the best place for a solar panel or plants.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Sunshine meter with an LED bar: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
Sun sensorLight sensor
Sun levelLED bar

Wiring, pin by pin

Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.

#ColourFrom (part and leg)To (header pin)
1OrangeSun sensor - 3.3 VPin 1 (3V3)
2ColouredSun sensor - signalPin 7 (GPIO4)
3BlackSun sensor - GNDPin 6 (GND)
4ColouredSun level - SignalPin 29 (GPIO5)
5ColouredSun level - SignalPin 31 (GPIO6)
6ColouredSun level - SignalPin 33 (GPIO13)
7ColouredSun level - SignalPin 35 (GPIO19)
8ColouredSun level - SignalPin 37 (GPIO26)
9BlackSun level - GNDPin 9 (GND)

Python code (gpiozero)

from gpiozero import LightSensor, LEDBarGraph
from time import sleep

sun = LightSensor(4)
bar = LEDBarGraph(5, 6, 13, 19, 26)

while True:
    level = sun.value                         # 0.0 = dark, 1.0 = full sun
    bar.value = level                         # the bar shows the same fraction
    print("Sunshine: %d %%" % (level * 100))
    sleep(0.5)

How the code works

You will learn

  • Reading a light sensor as a number
  • Showing a value on an LED bar
  • Mapping 0 to 1 onto a display

Try this

  • Slide the Daylight slider and watch the bar.
  • Print the best (maximum) reading of the last 20 readings.
  • Light a buzzer when the sun is above 90 %.

On a real Pi

  • An LDR is not accurate and changes with temperature. For real solar work use a lux sensor (BH1750) or a small reference solar cell.
  • Shade the LDR from other lamps, or the reading means nothing.
  • The Pi reads an LDR by timing a capacitor. A 1 uF capacitor is needed.

Parts to buy

The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
4330 ohm resistor, 1/4 W
One per LED
5Robu.in Amazon.in ThinkRobotics
5LED bar graph module (5 LEDs)1Robu.in Amazon.in ThinkRobotics
6LDR (GL5528) photoresistor1Robu.in Amazon.in ThinkRobotics
71 µF capacitor1Robu.in Amazon.in ThinkRobotics
8Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
9Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 43. Cold-room temperature alarmAll projects45. Two-hand press control →

This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.