MB-1mBotBeginner

First Program with mBot: Light Up the Onboard RGB LEDs

Upload your first code to the mBot with the Arduino IDE. The two RGB LEDs on the robot's board (mCore) light up red, green and blue together, then the right one turns yellow, the left one purple, and both go off. No wires or extra parts are needed; just connect the mBot to the computer over USB. setColor picks the color and show sends it to the LEDs.

0.0(0)
First Program with mBot: Light Up the Onboard RGB LEDs
The first step from block coding to typing real code for your mBot. You learn how to choose the board and port for the mBot in the Arduino IDE, install the Makeblock library, light the two onboard RGB LEDs from code, mix colors with red, green and blue values, color the two LEDs together or one by one, set the brightness, and build a sequence with waiting times. Every later mBot project builds on this.

Video

Circuit Diagram

First Program with mBot: Light Up the Onboard RGB LEDs - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   First Program with mBot: Light Up the Onboard RGB LEDs
4// ─────────────────────────────────────────
5// The two RGB LEDs on the mBot's board (mCore) light up red, green and
6// blue together, then the right one turns yellow, the left one purple, and
7// both go off. No extra parts or wires are needed; the code is uploaded
8// over the USB cable.
9//
10// Pins:
11//   D13 (PORT_7) -> 2 onboard RGB LEDs
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
17const uint8_t RIGHT_LED = 1;              // Right LED
18const uint8_t LEFT_LED = 2;               // Left LED
19
20// ============ SETTINGS ============
21const uint8_t LEVEL = 60;                 // Brightness, 0-255
22const unsigned long COLOR_MS = 1000;      // Time for each color (ms)
23const unsigned long OFF_MS = 500;         // Dark pause (ms)
24
25MeRGBLed led(PORT_7, LED_COUNT);
26
27// Writes the color to the chosen LED and shows it at once. The LED does not change until show() is
28// called.
29void showColor(uint8_t index, uint8_t red, uint8_t green, uint8_t blue) {
30  led.setColor(index, red, green, blue);
31  led.show();
32}
33
34void setup() {
35  showColor(ALL_LEDS, 0, 0, 0);           // Start with the LEDs off
36}
37
38void loop() {
39  showColor(ALL_LEDS, LEVEL, 0, 0);       // Red
40  delay(COLOR_MS);
41  showColor(ALL_LEDS, 0, LEVEL, 0);       // Green
42  delay(COLOR_MS);
43  showColor(ALL_LEDS, 0, 0, LEVEL);       // Blue
44  delay(COLOR_MS);
45
46  // The two LEDs can be colored one by one too: yellow on the right, purple on the left.
47  led.setColor(RIGHT_LED, LEVEL, LEVEL, 0);
48  led.setColor(LEFT_LED, LEVEL, 0, LEVEL);
49  led.show();
50  delay(COLOR_MS);
51
52  showColor(ALL_LEDS, 0, 0, 0);           // Turn off
53  delay(OFF_MS);
54}
55

FAQ

Why are the LEDs not at full brightness?

LEVEL is 60 in the code. These LEDs are very bright; the top value of 255 is dazzling on a desk and draws more current. 60 is easy to see and causes no trouble while the robot is powered over USB. If you want more light, raise LEVEL; it cannot go above 255.

Which one is LED number 1?

In the code 0 picks both LEDs, 1 the right LED and 2 the left LED. Right and left are from the robot's own point of view: stand behind it, face the same way, and the LED on your right is number 1. Seen from the front they look swapped.

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.