MB-11mBotBeginner

mBot Dark Alarm: A Buzzer That Sounds When the Light Goes Out

The light sensor on the mBot's board measures the ambient light. When it gets dark the buzzer sounds with repeated beeps; when it gets bright it goes quiet. No extra parts are needed. The start and stop limits differ (300 and 450); the reading is printed to the Serial port.

0.0(0)
mBot Dark Alarm: A Buzzer That Sounds When the Light Goes Out
An alarm project that joins the light sensor and the buzzer. You learn to read the sensor and decide with two limits, to keep the state of the alarm in a variable, to build a broken alarm sound from a beep followed by silence, and that the program waits while a note plays, so the sensor is not read during that time. Put the robot on a windowsill: it sounds when night falls or when someone covers the sensor.

Video

Circuit Diagram

mBot Dark Alarm: A Buzzer That Sounds When the Light Goes Out - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   mBot Dark Alarm: A Buzzer That Sounds When the Light Goes Out
4// ─────────────────────────────────────────
5// The light sensor on the mBot's board measures the ambient light. When it
6// gets dark the buzzer sounds with repeated beeps; when it gets bright it
7// goes quiet. No extra parts are needed.
8//
9// Pins:
10//   D8 -> Onboard buzzer
11//   A6 (PORT_6) -> Onboard light sensor
12
13#include <MeMCore.h>
14
15// ============ SETTINGS ============
16const int DARK_LIMIT = 300;               // Below this value the alarm starts
17const int BRIGHT_LIMIT = 450;             // Above this value the alarm stops
18const uint16_t ALARM_HZ = 880;            // Beep frequency (Hz)
19const unsigned long BEEP_MS = 200;        // Length of one beep (ms)
20const unsigned long GAP_MS = 200;         // Silence between two notes (ms)
21const unsigned long READ_MS = 100;        // Time between readings (ms)
22
23MeBuzzer buzzer;
24MeLightSensor lightSensor(PORT_6);
25bool alarmOn = false;                     // Is the alarm sounding right now
26
27void setup() {
28  Serial.begin(9600);
29}
30
31void loop() {
32  int light = lightSensor.read();         // 0 dark, 1023 very bright
33  Serial.println(light);
34  // The alarm starts only below the dark limit and stops only above the bright limit.
35  if (!alarmOn && light < DARK_LIMIT) {
36    alarmOn = true;
37  } else if (alarmOn && light > BRIGHT_LIMIT) {
38    alarmOn = false;
39  }
40  if (alarmOn) {
41    buzzer.tone(ALARM_HZ, BEEP_MS);
42    delay(GAP_MS);
43  } else {
44    delay(READ_MS);
45  }
46}
47

FAQ

Why does the alarm sound in short beeps?

In every pass the code plays a 200 millisecond beep and then stays quiet for 200 milliseconds. Because this pair repeats without end you hear a broken alarm sound. For a faster alarm make BEEP_MS and GAP_MS smaller; for a continuous sound set GAP_MS to 0. ALARM_HZ sets how high the tone is.

Can the alarm sound in the light instead of the dark?

Yes. A drawer or cupboard alarm works like that: it sounds when the door opens and light comes in. To do it, turn the two comparisons around: the alarm starts when the light rises above BRIGHT_LIMIT and stops when it falls below DARK_LIMIT. The details are in this project's task.

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.