Arque Technologies STEM Robotics Series

Night Light& Battery Tester

Two beginner Arduino projects from one starter kit: a night light that switches itself on when it gets dark, and a tester that tells you if an AAA cell is good, weak or flat.

You’ll use
  • Arduino UNO
  • LDR module
  • RGB LED
  • Buzzer
  • Resistors
Try both projects
RGB LED LED Buzzer LDR module
nightday
switches when very darkswitches early
Play the buzzer sound
LDR DO pin –
Night light –

Meet the parts

Everything here comes from the kit. Both projects share the same breadboard and output parts, so you wire most of it once.

PartJobUsed in
Arduino UNO (SMD, FT232 USB)Runs the codeBoth
USB cable, A to BUploads code and powers the ArduinoBoth
400-point breadboardHolds the parts with no solderingBoth
Male-to-male jumper wiresConnect the Arduino to the breadboardBoth
RGB LED, 5 mm, 4 legsGlowing night light, or the test result colourBoth
LED, 5 mmDaylight indicator, or “battery connected”Both
Buzzer B-10Chirps at nightfall, or beeps the resultBoth
Resistor pack: four 220 ΩProtect each LED colour and the LEDBoth
LDR moduleTells light from darkProject 1
Resistor pack: two 10 kΩHalve the battery voltage for A0Project 2
AAA 1.5 V batteryThe battery being testedProject 2
How to identify the LED legs, the RGB LED legs, resistor colour bands and LDR module pins + long− short flat edgeis − LED RcommonGB RGB LEDlongest leg = common 220 Ωred red brown 10 kΩbrown black orange Resistors VCCGNDDOAO screw sets sensitivity LDR module Pin orders vary between makers: always read the labels printed on your parts.
No 220 Ω in your pack? Anything from 220 Ω to 470 Ω works for the LEDs; higher values just glow dimmer.

The AAA battery can’t power the ArduinoAn UNO needs 5 V from USB. In this kit the AAA cell is what Project 2 tests, so keep the USB cable plugged in for both projects.

1

Set up the software

  1. Install the Arduino IDENo extra libraries are needed for these projects.
  2. Connect the UNOPlug in the USB A-to-B cable. This board uses an FT232 USB chip. If no port appears, install the FTDI driver.
  3. Pick the board and portChoose Tools → Board → Arduino Uno, then the Port that appeared when you plugged it in.
2

Build the base circuit

Unplug the USB cable while wiring. This layout has room for both projects at once. Pick a group to see only its parts and wires.

Breadboard layout for the base circuit, the night light add-on and the battery tester add-on+−+−115510101515202025253030abcdefghijArduino UNOD9D10D6D7A0D8D25VGNDR−GBRGB LED+−LED+LDR moduleVCCGNDDOAOAAA 1.5V+−Buzzer
Holes in the same numbered column (a–e, or f–j) are joined inside the board. The resistors bridge the middle gap. Pins on the Arduino are drawn in a row for clarity, so follow the labels.

Connection table

Arduino pinGoes toThrough
D9RGB red leg220 Ω
D10RGB green leg220 Ω
D6RGB blue leg220 Ω
GND railRGB common (longest) legwire
D7LED long leg (+)220 Ω
GND railLED short leg (−)wire
D8Buzzer + legdirect
D2LDR module DOwire
5V / GND railsLDR module VCC / GNDwire
A0Middle of the two 10 kΩ resistorswire
GND railSecond 10 kΩ, and battery −wire
5V, GNDBottom + and − rails, bridged to the top railswire

Why blue is on D6, not D11The buzzer’s tone() borrows the timer that dims pins 3 and 11. Keeping the RGB LED on D9, D10 and D6 lets it glow smoothly while the buzzer beeps.

Test the base circuit

Upload this test, then open the Serial Monitor at 9600 baud. The RGB LED should show red, green, blue, then the LED lights and the buzzer beeps.

base_test.ino
// BASE CIRCUIT TEST: RGB LED, LED and buzzer
const int RED = 9, GREEN = 10, BLUE = 6;
const int LED = 7, BUZZER = 8;
const bool COMMON_ANODE = false;  // true if the RGB long leg goes to 5V

void rgb(int r, int g, int b) {
  if (COMMON_ANODE) { r = 255 - r; g = 255 - g; b = 255 - b; }
  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);
}

void setup() {
  pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT);
  pinMode(LED, OUTPUT); pinMode(BUZZER, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.println("Red");   rgb(255, 0, 0); delay(800);
  Serial.println("Green"); rgb(0, 255, 0); delay(800);
  Serial.println("Blue");  rgb(0, 0, 255); delay(800);
  rgb(0, 0, 0);
  Serial.println("LED and beep");
  digitalWrite(LED, HIGH);
  tone(BUZZER, 1000, 200);
  delay(800);
  digitalWrite(LED, LOW);
  delay(400);
}
  1. No colours at all?Your RGB LED may be common anode. Move its long-leg wire from the GND rail to the + rail and set COMMON_ANODE = true in every sketch.
  2. Colours in the wrong order?Leg order varies between makers. Swap the wires on D9, D10 and D6 until “Red” shows red.
  3. LED stays dark?Turn it round: the long leg goes towards the resistor and D7.

Before you plug in

0 of 5 checked

3

Project 1: Smart night light

An LDR (light-dependent resistor) changes its resistance with light. The module compares it with the setting on its small screw and switches its DO pin, so the Arduino just reads HIGH or LOW.

In bright light the LDR module's DO pin is LOW and its LED is on; in the dark DO is HIGH and the night light turns on DO = LOWmodule LED on BrightLDR resistance lowNight light OFF, plain LED on DO = HIGHmodule LED off DarkLDR resistance highNight light ON, rainbow glow
On most modules DO goes HIGH in the dark. If yours works the other way, change DARK to LOW.
  1. Add the LDR modulePlug it into row a, columns 24–27. Wire VCC to the + rail, GND to the − rail and DO to D2. AO isn’t needed.
  2. Upload the night light sketchKeep the USB cable plugged in.
  3. Set the sensitivityCover the sensor with your hand and turn the module’s screw until its DO LED goes off in shadow and on in light.
  4. Try itCover the sensor or switch off the room light. After a second the buzzer chirps and the RGB LED starts a slow rainbow. The plain LED shows it’s daytime.
night_light.ino
// SMART NIGHT LIGHT: LDR module + RGB LED + LED + buzzer
const int LDR_DO = 2;               // LDR module DO pin
const int RED = 9, GREEN = 10, BLUE = 6;
const int LED = 7, BUZZER = 8;
const bool COMMON_ANODE = false;    // true if the RGB long leg goes to 5V
const int DARK = HIGH;              // DO level when it is dark
const unsigned long SETTLE = 1000;  // ms it must stay dark or light
const int BRIGHTNESS = 150;         // night light brightness, 0-255

bool night = false;
unsigned long changedAt = 0;
int hue = 0;

void rgb(int r, int g, int b) {
  r = map(r, 0, 255, 0, BRIGHTNESS);
  g = map(g, 0, 255, 0, BRIGHTNESS);
  b = map(b, 0, 255, 0, BRIGHTNESS);
  if (COMMON_ANODE) { r = 255 - r; g = 255 - g; b = 255 - b; }
  analogWrite(RED, r); analogWrite(GREEN, g); analogWrite(BLUE, b);
}

void showHue(int h) {                // 0-359 around the colour wheel
  int x = (h % 120) * 255 / 120;
  if (h < 120)      rgb(255 - x, x, 0);
  else if (h < 240) rgb(0, 255 - x, x);
  else              rgb(x, 0, 255 - x);
}

void setup() {
  pinMode(LDR_DO, INPUT);
  pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT);
  pinMode(LED, OUTPUT); pinMode(BUZZER, OUTPUT);
  rgb(0, 0, 0);
  Serial.begin(9600);
  Serial.println("Night light ready");
}

void loop() {
  bool darkNow = (digitalRead(LDR_DO) == DARK);

  if (darkNow != night) {                       // light level changed
    if (changedAt == 0) changedAt = millis();
    if (millis() - changedAt >= SETTLE) {       // and stayed changed
      night = darkNow;
      changedAt = 0;
      if (night) {
        Serial.println("Dark: night light ON");
        tone(BUZZER, 880, 120); delay(160);
        tone(BUZZER, 660, 160);
      } else {
        Serial.println("Light: night light OFF");
      }
    }
  } else {
    changedAt = 0;
  }

  digitalWrite(LED, night ? LOW : HIGH);        // LED on in daylight
  if (night) {
    hue = (hue + 1) % 360;                      // slow rainbow
    showHue(hue);
  } else {
    rgb(0, 0, 0);
  }
  delay(20);
}
SettingDefaultWhat it changes
DARKHIGHThe DO level that means “dark”
SETTLE1000 msHow long it must stay dark or light before switching, so a passing shadow doesn’t trigger it
BRIGHTNESS150Night light brightness, 0–255
4

Project 2: AAA battery tester

The Arduino’s A0 pin measures voltage from 0 to 5 V as a number from 0 to 1023. Two equal resistors split the battery voltage in half. This keeps the pin safe if the battery is connected the wrong way round, and the code doubles the reading back.

Voltage divider: battery plus through a 10 kilohm resistor to A0, and a second 10 kilohm resistor from A0 to ground, halves the voltage AAA 1.5V +− R1 10 kΩ R2 10 kΩ A0 GND 1.50 V 0.75 V A0 reads153 × 5 ÷ 10230.75 V × 21.50 V
A fresh alkaline cell reads about 1.5–1.6 V. The tester measures it without a load, so a tired cell can still look better than it is.
  1. Add the two 10 kΩ resistorsOne from g15 to g19, one from i15 to the bottom − rail. Wire A0 to f15.
  2. Connect the batteryPress a wire from j19 onto the battery’s + end, and a wire from the − rail onto its flat − end. Tape helps them stay put.
  3. Upload the battery tester sketchOpen the Serial Monitor to see the voltage.
  4. Read the resultThe LED lights to show a battery is connected, and the RGB LED and beeps give the result.
Result bands: below 0.3 V no battery, 0.3 to 1.2 V replace, 1.2 to 1.4 V weak, 1.4 V and above good noneREPLACE: red, 3 low beeps WEAKGOOD 0 V0.31.21.41.7 V yellow, 2 beepsgreen, 1 beep
battery_tester.ino
// AAA BATTERY TESTER: 1.5 V cells only
const int BATT = A0;                // between the two 10k resistors
const int RED = 9, GREEN = 10, BLUE = 6;
const int LED = 7, BUZZER = 8;
const bool COMMON_ANODE = false;    // true if the RGB long leg goes to 5V
const float VREF = 5.0;             // the UNO's 5 V supply

const float GOOD = 1.40, WEAK = 1.20, NONE = 0.30;   // volts
int lastState = -1;                 // 0 none, 1 replace, 2 weak, 3 good

void rgb(int r, int g, int b) {
  if (COMMON_ANODE) { r = 255 - r; g = 255 - g; b = 255 - b; }
  analogWrite(RED, r); analogWrite(GREEN, g); analogWrite(BLUE, b);
}

float readBattery() {
  long sum = 0;
  for (int i = 0; i < 20; i++) {    // average 20 readings
    sum += analogRead(BATT);
    delay(5);
  }
  float pinVolts = (sum / 20.0) * VREF / 1023.0;
  return pinVolts * 2.0;            // two equal resistors halve the voltage
}

void beeps(int count, int freq) {
  for (int i = 0; i < count; i++) {
    tone(BUZZER, freq, 120);
    delay(220);
  }
}

void setup() {
  pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT);
  pinMode(LED, OUTPUT); pinMode(BUZZER, OUTPUT);
  Serial.begin(9600);
  Serial.println("Battery tester ready");
}

void loop() {
  float v = readBattery();
  int state = (v < NONE) ? 0 : (v < WEAK) ? 1 : (v < GOOD) ? 2 : 3;

  if (state == 0)      rgb(0, 0, 0);       // nothing connected
  else if (state == 1) rgb(255, 0, 0);     // red: replace
  else if (state == 2) rgb(255, 120, 0);   // yellow: weak
  else                 rgb(0, 255, 0);     // green: good
  digitalWrite(LED, state > 0 ? HIGH : LOW);  // a battery is connected

  Serial.print("Battery: ");
  Serial.print(v, 2);
  Serial.print(" V  ");
  if (state == 0)      Serial.println("No battery");
  else if (state == 1) Serial.println("REPLACE");
  else if (state == 2) Serial.println("WEAK");
  else                 Serial.println("GOOD");

  if (state != lastState && state > 0) {   // beep once per new result
    if (state == 3) beeps(1, 1500);
    if (state == 2) beeps(2, 1000);
    if (state == 1) beeps(3, 400);
  }
  lastState = state;
  delay(300);
}

Only test single 1.5 V cellsUse AA or AAA cells only. Never connect a 9 V battery, a lithium battery pack or a phone battery, and never join the battery to the 5V rail. Rechargeable NiMH cells are 1.2 V when full, so they show WEAK even when charged.

Troubleshooting

No port appears in the Arduino IDE
  • Try another USB cable or port. Some cables only charge.
  • Install the FTDI driver (link in step 1), then plug the board in again.
The RGB LED shows the wrong colours, or none
  • No light at all: it may be common anode. Move the long leg to the + rail and set COMMON_ANODE = true.
  • Wrong colours: swap the wires on D9, D10 and D6.
  • One colour missing: check that leg’s resistor sits in the same column.
The buzzer is silent or only clicks
  • Swap its legs: the + mark goes to D8.
  • Both active and passive buzzers sound with tone(); an active one just sounds rougher.
The night light never turns on
  • Watch the module’s DO LED while covering the sensor, and adjust its screw until it switches.
  • If the light comes on in daylight instead, change DARK to LOW.
The night light flickers at dusk

Increase SETTLE to 2000 or 3000 ms, or turn the screw a little so it switches in a darker room.

The tester always says “No battery”
  • Press the wires firmly on the battery ends and check A0 goes to f15.
  • Check the battery − wire reaches the − rail.
The voltage looks too high or too low
  • Both resistors must be 10 kΩ (brown black orange).
  • USB power isn’t exactly 5 V. If you know the real value, set VREF to it.