Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Mega Components

ELEGOO MEGA · LESSON 26

Photocell

Change the number of lit bar LEDs with a photocell divider.

Turn an analog divider reading into an eight-bit LED pattern.

Parts List

  • ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
  • An 830-contact solderless breadboard joins inserted leads through hidden spring strips so circuits can be rebuilt without solder. Full-size rectangular board, center channel, numbered five-hole terminal groups and side power rails. Counted contact holes are not component units.
  • A green indicator LED converts forward current into visible light; use it for status indication with a series resistor. Green two-lead dome. Lead length may have been trimmed; confirm polarity with diode test.
  • A red indicator LED converts forward current into visible light; use it for status indication with a series resistor. Red two-lead dome; longer lead normally anode, flat edge normally cathode. Lead length may have been trimmed; confirm polarity with diode test.
  • A yellow indicator LED converts forward current into visible light; use it for status indication with a series resistor. Yellow two-lead dome. Lead length may have been trimmed; confirm polarity with diode test.
  • A blue indicator LED converts forward current into visible light; use it for status indication with a series resistor. Blue two-lead dome. Lead length may have been trimmed; confirm polarity with diode test.
  • 1 kΩ resistor provides a fixed opposition to current. Signal current limiting and transistor base drive is a common use when its unverified power and tolerance limits fit the circuit. Small axial body with one wire at each end. Find the labeled value compartment and measure an isolated loose part with a meter; the shared inventory image is a form reference.
  • 74HC595 accepts eight bits serially and latches them onto eight parallel logic outputs, expanding output control with a few GPIO lines. 16-pin DIP marked 74HC595/74HC595N with notch or pin-one dot. HC and HCT variants have different input thresholds.
  • 100 nF nonpolar ceramic capacitor stores electric-field energy. Local IC decoupling and short high-frequency supply disturbances are appropriate uses when voltage and circuit requirements are checked. Small non-polar disc, often marked 104; 104 means 100 nF, not 104 pF. Read the value and rating rather than estimating from body size.
  • Male-to-male jumper leads make removable point-to-point connections for low-current prototype signals. Pins at both ends; count individual leads rather than ribbon groups. Check intact insulation and snug contacts before use.
  • A photoresistor element changes resistance with illumination for relative light/dark detection. Small round disc with a visible serpentine track and two leads. The exposed serpentine face distinguishes an LDR from a smooth encapsulated photodiode.

Assembly

  1. Disconnect USB and use the original LED layout, replacing the eight LED resistors with 1 kΩ and adding the IC bypass capacitor.
  2. Check the notch, MR→5 V, OE→GND and every LED polarity before reconnecting.
  3. Connect the photocell toward 5 V, the additional fixed 1 kΩ toward GND, and their junction to A0.
Illustration © ELEGOO. All rights reserved.
Illustration © ELEGOO. All rights reserved. Open full-size ↗

Reviewed connections

FromToNote
Mega D12,D11,D974HC595 14 DS,12 ST_CP,11 SH_CPData,latch,clock
74HC595 16 and 10 → 5 V; 8 and 13 → GNDPower, clear and enableNotch identifies pin 1; pin 9 Q7′ stays open
Q0..Q7 (15,1,2,3,4,5,6,7)One 1 kΩ then one LED anode per outputEach LED cathode → GND; replace all eight pictured 220 Ω values
100 nF across IC 16 / 8Supply bypassPlace close to the IC
Photocell → 5 V; fixed 1 kΩ → GNDJunction → Mega A0This ninth 1 kΩ is separate from the eight LED resistors

Source-reviewed wiring. Hardware not bench-tested; firmware uploads have not been performed.

Code

PlatformIO configuration
platformio.ini
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
main.cpp
#include <Arduino.h>
const uint8_t LATCH_PIN = 11;  // ST_CP, IC pin 12, copies shifted bits to visible outputs.
const uint8_t CLOCK_PIN = 9;   // SH_CP, IC pin 11, advances the serial shift register.
const uint8_t DATA_PIN = 12;   // DS, IC pin 14, supplies one bit for each clock edge.
uint8_t leds = 0;  // Eight bits remember which LEDs should be on.
void updateShiftRegister() {
  digitalWrite(LATCH_PIN, LOW);
  // LSBFIRST sends bit 0 first; after eight clocks it reaches Q7, not Q0.
  shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, leds);
  digitalWrite(LATCH_PIN, HIGH);  // All outputs change together after the complete byte.
}

const uint8_t LIGHT_PIN = A0;
void setup() {

  pinMode(LATCH_PIN, OUTPUT); pinMode(DATA_PIN, OUTPUT); pinMode(CLOCK_PIN, OUTPUT);

}
void loop() {
  const int reading = analogRead(LIGHT_PIN);  // LDR to +5 V, fixed 1 kΩ to GND.
  uint8_t count = reading / 57;  // Retain the supplied threshold scaling, not a lux conversion.
  if (count > 8) count = 8;
  leds = 0;
  for (uint8_t i = 0; i < count; ++i) {
    leds |= uint8_t(1U << i);  // Shift a 1 into place; OR keeps bits that are already set.
  }
  updateShiftRegister();
}

Source: SOURCE_RECONCILED. Compile: COMPILE_VERIFIED. Hardware: NOT_TESTED. Upload: NOT_PERFORMED.

Expected behavior

More light lowers LDR resistance, raises A0 and lights more LEDs. reading/57 saturates at eight LEDs; this is not a lux meter.

Experiments & Challenges

  • Shade the photocell, then use a fixed light distance to compare the bar.

Troubleshooting

  • Wrong sequence: check data/latch/clock order and LSBFIRST; missing LEDs usually indicate polarity or an open resistor path.