Skip to content

Search the documentation

Kit
Firmware

Night light — Smart Home

Cover the little light sensor with your hand and the RGB LED comes on, like a lamp that knows when it is dark. Take your hand away and it goes out again.

The board also tells you, once at the start, how much battery it has left.

This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Home.

The blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
Read the program as text
  • MoBot Start
  • Print to serial "Night light starting"
  • Print to serial (create text with "Battery (volts): " Read Battery Level)
  • repeat forever
    • Print to serial (create text with "Light: " Read Light Sensor (LDR))
    • if (Read Light Sensor (LDR) < 800)
      • Set RGB LED R: 255 G: 255 B: 255 .
    • else
      • Set RGB LED R: 0 G: 0 B: 0 .
    • wait 1 seconds
  • A Smart Home board with the LDR (the light sensor — the small round part that looks like a squashed snail shell) and the RGB LED.
  • A USB cable and the serial monitor.
  1. Open 04-night-light.mobot in MoBot.
  2. Plug in, pick the port, press Send to Device.
  3. Open the serial monitor.
  4. Cup your hand over the light sensor.
[MOBOT] Night light starting
[MOBOT] Battery (volts): 4.02
[MOBOT] Light: 2400
[MOBOT] Light: 2410
[MOBOT] Light: 340 <- hand over the sensor
  • Light is a raw reading from 0 to 4095. Bigger means brighter. A lit classroom is usually 1500–3000; your hand over the sensor drops it to a few hundred; a torch shone at it pushes it near 4095.
  • The moment the number goes under 800, the RGB LED turns white. Lift your hand and it goes dark again, within a second.
  • Battery (volts) should be somewhere around 3.3 to 4.2 on a charged battery. On USB power with no battery fitted it may read near 0 or something meaningless — that is not a fault, there is simply nothing to measure.

If the LED never comes on, read the printed numbers with your hand in place and type a number just above that into the 800 block. Rooms differ enormously.

If the LED never goes off, your room is darker than 800. Raise the number.

On a board with firmware older than 2.0, red and green come out swapped (see 03-gas-alarm for the check). White is 255/255/255 — three equal numbers, each colour from 0 to 255 — so swapping two of them changes nothing at all. A night light that must be believed is best painted in a colour the swap cannot touch.

Change it to R:0 G:0 B:255 and you get a blue night light, also safe. Ask for red on such a board and you will get green.

block what it does
iot_ldr Reads the light sensor. 0 (dark) to 4095 (bright).
iot_battery Reads the battery voltage, in volts, with one decimal place.
controls_if + logic_compare “If it is darker than 800 … otherwise …”
iot_rgb_color Sets the LED colour, 0–255 per colour.
iot_print, text_join The commentary.

The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.

04-night-light.firmware-3.main.py
# Night light — iot (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 04-night-light.mobot and press Send to Device. It is GENERATED from the blocks;
# editing this file changes nothing. Edit the blocks instead.
# The firmware 2.x version is 04-night-light.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Night light starting')
print("[MOBOT]", ('Battery (volts): ' + str(home.battery.voltage())))
while True:
print("[MOBOT]", ('Light: ' + str(home.light_sensor.level())))
if home.light_sensor.level() < 800:
home.rgb_led.set_color(255, 255, 255)
else:
home.rgb_led.set_color(0, 0, 0)
time.sleep(1)
  • Turn it into a day light: change < to > and the LED comes on when the room is bright.
  • Print the battery inside the loop as well, and watch it fall while the board runs on battery for an afternoon.
  • Make the wait 0.1 and wave your hand over the sensor — the LED flickers like a candle.