Guide no. 86
The little DFRobot Maqueen Lite robot is programmed from MakeCode with the official extension: a first drive, line following and the ultrasonic sensor, in three short programs.
By Ada Intelisano · informaticait.it technical team
- The Maqueen Lite comes assembled: you need a micro:bit, three AAA batteries and a browser.
- In MakeCode you add DFRobot's “maqueen” extension: the blocks for the motors, the LEDs and the sensors.
- The line sensors say 1 on white and 0 on black: the robot turns toward the sensor that sees the line.
- The ultrasonic sensor measures the distance in centimeters: below 30 the robot changes course.
A little robot that starts when you press a button, follows a black line and steers clear of obstacles. Its brain is the micro:bit, the board designed for schools; its body is the DFRobot Maqueen Lite, which comes already assembled, with motors, wheels and sensors. You program it from the browser with MakeCode, using blocks or typing, and there is nothing to install.
1What you need
- the DFRobot Maqueen Lite robot, with the blue or white skin. The box contains the robot with motors, wheels and battery holder already fitted, the ultrasonic sensor and the map with the track;
- a micro:bit, V2 or V1: it is not in the box. In our shop you can find it in the SparkFun Inventor's Kit for micro:bit v2;
- three 1.5 V AAA alkaline batteries (not included) and a micro-B USB cable;
- a computer with a browser: with Chrome or Edge the program goes straight into the board.
2Meet the robot
- The micro:bit reads the sensors and controls everything from the pins of its edge connector. It does not drive the motors directly, though: it sends the orders to a chip on the robot's board, over the I²C bus (guide no. 22).
- At the front there are the two red LEDs, the infrared receiver and the socket for the ultrasonic sensor. Underneath are the two line sensors and four colored LEDs.
- The Line-Key button is for calibrating the line sensors, but DFRobot already calibrates them at the factory: leave it alone for now.
3Get the robot ready
- Set the Power switch to OFF, then put the three batteries in the battery holder under the robot, with + and − the way they are marked inside: DFRobot recommends this.
- Plug the ultrasonic sensor into the socket at the front, with its two cylinders facing forward.
- Slide the micro:bit into the slot, with the display and the buttons facing the front, as in the DFRobot photos. Hold it by the edges and do not rest metal objects on the circuits (micro:bit safety advice).
4MakeCode and the extension
- Open makecode.microbit.org and create a new project.
- From the gear icon at the top right choose Extensions and search for maqueen, or paste
https://github.com/DFRobot/pxt-maqueen. A green group of blocks appears, “Maqueen v4”. - The programs in this guide are written as text. At the top choose JavaScript, delete what is there and paste; when you go back to Blocks, MakeCode turns them into blocks.
- To upload them press Download: with Chrome or Edge you connect the micro:bit once and the program goes across directly; with other browsers you download a .hex file and drag it onto the MICROBIT drive. While it uploads, the status LED of the micro:bit flashes.
5A first drive
// Press A: headlights on, one second forward, then stop input.onButtonPressed(Button.A, function () { basic.showArrow(ArrowNames.North) maqueen.writeLED(maqueen.LED.LEDLeft, maqueen.LEDswitch.turnOn) maqueen.writeLED(maqueen.LED.LEDRight, maqueen.LEDswitch.turnOn) maqueen.motorRun(maqueen.Motors.All, maqueen.Dir.CW, 100) basic.pause(1000) maqueen.motorStop(maqueen.Motors.All) maqueen.writeLED(maqueen.LED.LEDLeft, maqueen.LEDswitch.turnOff) maqueen.writeLED(maqueen.LED.LEDRight, maqueen.LEDswitch.turnOff) basic.showIcon(IconNames.Happy) }) // Press B: spin on the spot to the right for half a second input.onButtonPressed(Button.B, function () { basic.showArrow(ArrowNames.East) maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CW, 80) maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CCW, 80) basic.pause(500) maqueen.motorStop(maqueen.Motors.All) basic.showIcon(IconNames.Happy) }) basic.showIcon(IconNames.Happy)
motorRun(motor, direction, speed): M1 is the left motor, M2 the right one, All both of them; CW is forward (“Forward” in the block), CCW backward; the speed goes from 0 to 255.- To spin on the spot, one motor goes forward and the other backward.
- Do your tests on the floor, not on the table: a robot that sets off suddenly can easily fall.
- It does not move but the micro:bit responds: is Power set to ON? If the power LED is on and the motors stay still, according to DFRobot the batteries may be flat.
- Red blocks or errors: the maqueen extension is not loaded in the project.
6Follow the line
Each line sensor says 1 when it sees white underneath and 0 when it sees black; under the robot two blue indicator LEDs light up on white and go out on black. The official DFRobot program keeps the line between the two sensors and goes slowly, because the map is small.
// Follow the black line: 1 = the sensor sees white, 0 = it sees black basic.forever(function () { let left = maqueen.readPatrol(maqueen.Patrol.PatrolLeft) let right = maqueen.readPatrol(maqueen.Patrol.PatrolRight) if (left == 1 && right == 1) { // the line runs between the sensors: straight on, slowly maqueen.motorRun(maqueen.Motors.All, maqueen.Dir.CW, 40) } else if (left == 0 && right == 1) { // the left sensor is on the black: turn left maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CCW, 10) maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CW, 40) } else if (left == 1 && right == 0) { // the right sensor is on the black: turn right maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CW, 40) maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CCW, 10) } })
- It runs off the track on the bends: lower the speed, from 40 to 30.
- First of all, look at the blue indicator LEDs: if they do not change when they go from white to black, the problem is the sensors, not the program.
7Steer clear of obstacles
// Avoid obstacles: below 30 cm it turns, randomly to the right or to the left
basic.forever(function () {
let distance = maqueen.Ultrasonic()
if (distance < 30) {
if (Math.randomBoolean()) {
maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CW, 50)
maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CW, 0)
} else {
maqueen.motorRun(maqueen.Motors.M1, maqueen.Dir.CW, 0)
maqueen.motorRun(maqueen.Motors.M2, maqueen.Dir.CW, 50)
}
basic.pause(800)
} else {
maqueen.motorRun(maqueen.Motors.All, maqueen.Dir.CW, 50)
}
})
maqueen.Ultrasonic() returns the distance in centimeters; when no echo comes back, the extension returns
500, which means “nothing ahead”. The logic is that of the official DFRobot example, written for an older version
of the extension and adapted here to the current one.
- It bumps into things: is the sensor pushed all the way in, with the cylinders facing forward?
- It turns even with no obstacles: something close is in front of the sensor, for example a cable.
- Light up the four colored LEDs: add the neopixel extension and create the strip on pin P15 with 4 LEDs, as in the DFRobot examples.
- Make it play a note when it sees an obstacle.
- Fit a Mechanic kit, for example the loader, and make it move something.
- the micro:bit controls the robot from the pins of its edge connector and, for the motors, over I²C;
- a line sensor says 1 on white and 0 on black;
- to turn, you just run the two motors at different speeds.
Useful products
Other guides
- Resistor colour code and tolerances
- N-channel and P-channel MOSFETs
- Bipolar transistor (BJT) or MOSFET?
- Relays: how to drive them and how much current they draw
- THT and SMD/SMT components
- XIAO board pinouts
- LEDs: forward voltage and series resistor
- The multimeter: measuring voltage and current
- Wemos D1 mini: first steps
- MOSFET module with Arduino and ESP32
- Relay module with Arduino and ESP32
- 3D printing filaments: PLA, PETG and fibre-filled versions
- Analog and digital: the differences
- ADS1115: 16-bit converter with Arduino and ESP32
- From bits to real values: converting sensor readings
- DS3231 RTC clock with Arduino and ESP32
- Linear or switching regulator: which one to choose
- Step-down, step-up and buck-boost: choosing the right module
- Voltage divider: reading voltages higher than 5 V or 3.3 V
- Pull-up, pull-down and bounce: wiring a button properly
- 3.3 V and 5 V logic levels: getting an ESP32 and an Arduino to talk
- The I²C bus: addresses, pull-ups and the scanner
- Lithium batteries: voltages, charging and protection
- ESP32 power consumption and deep sleep: how long the battery lasts
- Temperature sensors compared
- ESP32: which pins to use and which to avoid
- Getting started with the ESP32 in Arduino IDE 2 and PlatformIO
- SSD1306 OLED display and 16×2 I²C LCD with Arduino and ESP32
- PIR motion sensor and HC-SR04 ultrasonic sensor
- Date and time from the Internet with the ESP32: NTP, Italian time zone and daylight saving time
- MicroSD data logger: saving measurements to CSV
- Solar-powered ESP32: panel, charger and the numbers for Italy
- Web server on the ESP32: control a relay from your phone
- Your first LED: wire it up and make it blink
- The button: let Arduino decide
- The potentiometer: turn a knob and read its value
- Soft light: adjusting the brightness of an LED
- The buzzer: playing notes and melodies
- The traffic light: three LEDs in sequence
- The thermometer: reading the temperature with the TMP36
- The servo motor: moving an arm with Arduino
- The night light: the photoresistor and your first decision
- The relay: switching on a light bulb with Arduino
- The LCD display: making Arduino write
- The electronic dice: your first complete project
- The RGB LED: every color from three lights
- The joystick: two-axis control with a shield
- The LED ring: twelve colors from a single pin
- The DC motor: driving it with a MOSFET
- Soil moisture: the plant that asks for water
- The stepper motor: turn exactly as far as you want
- The parking sensor: faster and faster beeps
- The 7-segment display: big, bright numbers
- Automatic watering: the plant that waters itself
- The keypad: a lock with a secret code
- Networked weather station: values and chart on your phone
- A Telegram bot on the ESP32: your home from your phone
- MQTT with the ESP32: the language of connected devices
- Updating the ESP32 over Wi-Fi (OTA)
- ESP-NOW: two ESP32 boards talking without a router
- WebSocket with the ESP32: the LED that follows the slider
- Readings on a Google sheet, with deep sleep
- WiFiManager: choose the Wi-Fi from your phone
- Bluetooth LE with the ESP32: the phone without a network
- The camera in your browser with the ESP32-S3
- A portable Bluetooth speaker you build yourself
- Bluetooth desktop speakers, 2 × 20 W
- Stereo MP3 player with two buttons
- The shelf that plays music: a Visaton exciter
- How to solder well
- How much does it draw? Current and power with the INA219
- Load-cell scale with the HX711
- CO2 traffic light: when to open the windows
- RFID card door opener
- GPS: track logger on microSD
- LoRa: how far does it go? A range test with two boards
- Battery weather sign with an e-paper display
- Web radio with an ESP32 and an I2S amplifier
- Getting started with the Raspberry Pi Pico 2 in MicroPython
- Your first amplifier: the LM386 and a speaker
- Talking Arduino: the DFR0760 speech synthesis module
- The spectrum of your music on an LED matrix
- Drawing with 64 LEDs: the 8×8 matrix with the MAX7219
- A real voltage: the 12-bit MCP4725 DAC
- Your own macro keyboard: four NeoKey keys and the QT Py RP2040
- A two-way speaker with a Visaton woofer, tweeter and crossover
- A 120-LED NeoPixel strip, powered the right way
- A linear actuator to open a lid
- The heartbeat on Arduino: an ECG with the AD8232