mBot Night Light: LEDs That Turn On in the Dark with the Light Sensor
The light sensor on the mBot's board measures the ambient light. When it gets dark the two RGB LEDs light up warm white; when it gets bright they go off. No extra parts are needed. The on and off limits differ (300 and 450), so the lamp does not flicker at the edge. The reading is printed to the Serial port.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// mBot Night Light: LEDs That Turn On in the Dark with the Light Sensor
4// ─────────────────────────────────────────
5// The light sensor on the mBot's board measures the ambient light. When it
6// gets dark the two RGB LEDs light up warm white; when it gets bright they
7// go off. No extra parts are needed.
8//
9// Pins:
10// D13 (PORT_7) -> 2 onboard RGB LEDs
11// A6 (PORT_6) -> Onboard light sensor
12
13#include <MeMCore.h>
14
15const uint8_t LED_COUNT = 2; // Number of LEDs on the board
16const uint8_t ALL_LEDS = 0; // Number 0 picks both LEDs
17
18// ============ SETTINGS ============
19const int DARK_LIMIT = 300; // Below this value the lamp turns on
20const int BRIGHT_LIMIT = 450; // Above this value the lamp turns off
21const unsigned long READ_MS = 100; // Time between readings (ms)
22// The lamp color: red, green and blue (0-255). These values give a warm white.
23const uint8_t LAMP_RED = 60;
24const uint8_t LAMP_GREEN = 40;
25const uint8_t LAMP_BLUE = 12;
26
27MeRGBLed led(PORT_7, LED_COUNT);
28MeLightSensor lightSensor(PORT_6);
29bool lampOn = false; // Is the lamp on right now
30
31// Turns the lamp on or off and remembers the state.
32void setLamp(bool on) {
33 if (on) {
34 led.setColor(ALL_LEDS, LAMP_RED, LAMP_GREEN, LAMP_BLUE);
35 } else {
36 led.setColor(ALL_LEDS, 0, 0, 0);
37 }
38 led.show();
39 lampOn = on;
40}
41
42void setup() {
43 Serial.begin(9600);
44 setLamp(false); // Start with the lamp off
45}
46
47void loop() {
48 int light = lightSensor.read(); // 0 dark, 1023 very bright
49 Serial.println(light);
50 // While off, the lamp turns on only below the dark limit; while on, it turns off only above the
51 // bright limit.
52 if (!lampOn && light < DARK_LIMIT) {
53 setLamp(true);
54 } else if (lampOn && light > BRIGHT_LIMIT) {
55 setLamp(false);
56 }
57 delay(READ_MS);
58}
59FAQ
Why are there two limits instead of one?
With a single limit the reading wobbles just below and just above it when the light is right at the edge, and the lamp keeps switching on and off. With two limits the lamp turns on below 300 but waits for the reading to pass 450 before it turns off. In between, the lamp stays as it is. This method is called hysteresis and thermostats use it too.
How do I tune the limits for my own room?
Open the Serial Monitor in the Arduino IDE and set the speed to 9600; the code prints every reading there. Note the numbers with the room lit and with your hand covering the sensor. Put DARK_LIMIT a little above the dark number and BRIGHT_LIMIT a little below the bright number. The numbers change from room to room; the ones in the code are only a start.
Which board do I pick for the mBot in the Arduino IDE?
The mBot's board, the mCore, uses the same processor as the Arduino Uno (ATmega328P). Choose Arduino Uno under Tools > Board and the robot's port under Tools > Port. For the code to compile, the MakeBlock Drive Updated library must be installed from the Library Manager; the code pulls it in under the name MeMCore.h.
Does uploading this code erase the factory program on the robot?
Yes. The board holds one program at a time, so your code replaces the factory program and the ready-made modes, such as driving with the remote or line following, stop working. Bringing them back is easy: open mBlock, connect the mBot over USB and load the factory firmware again from the Firmware Update section.
