Skip to content

Search the documentation

Kit
Firmware

Check the battery — Smart Car

Robots go flat. This one measures its own battery every two seconds, prints the voltage, and lights LED 2 as a warning when it drops below 6.5 volts.

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

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 "Battery check. A number should appear every 2 seconds."
  • repeat forever
    • Print to serial (create text with "battery volts = " Read Battery voltage)
    • if (Read Battery voltage < 6.5)
      • Set LED 2 ON
      • Print to serial "LOW BATTERY - please change my batteries"
    • else
      • Set LED 2 OFF
    • wait 2 seconds
  • A standard Smart Car, the one with the DRV8833 motor chip. It has a battery sensor. The first Smart Cars, with the L298N motor chip, do not (see If you have an early Smart Car below).
  • Batteries in the robot’s holder (it takes six AA batteries), not flat, and the robot switched on. The sensor measures the batteries.
  • A USB cable to send the program and to read the serial monitor.
  • Nothing plugged into the SERVO 2 socket. Servo 2 uses GPIO 33, the pin the battery sensor reads: use one or the other.
  1. Put in batteries that are not flat.
  2. Open 14-battery-check.mobot, plug in, press Send to Device.
  3. Watch the serial monitor for half a minute.
  • A line every two seconds: [MOBOT] battery volts = followed by a number with several decimals.
  • The robot counts 6.0 volts as an empty battery and 9.0 volts as a full one. So the numbers you see should be somewhere around 6 to 9. The reading is not cut off at 9, so a set of fresh batteries may read a little above it.
  • LED 2 stays off while the reading is above 6.5.
  • When the reading is below 6.5 volts: LOW BATTERY - please change my batteries and LED 2 lights.

The test passes if the number is somewhere around 6 to 9 volts, and if LED 2 turns on when you raise the threshold above the reading.

Do not wait for the batteries to go flat: with healthy ones the warning half of this example never fires on its own. Change the 6.5 in the compare block to a number just above what you are reading. If the monitor says about 7.9, type 8.2, and press Send to Device again. LED 2 should light at once and the low-battery line should appear. Then put 6.5 back.

block what it does
mobot_batvoltage the battery voltage, as a number of volts.
logic_compare is it below 6.5?
mobot_ledon the warning light.

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.

14-battery-check.firmware-3.main.py
# Check the battery — robotics (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 14-battery-check.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 14-battery-check.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Battery check. A number should appear every 2 seconds.')
while True:
print("[MOBOT]", ('battery volts = ' + str(robot.battery.voltage())))
if robot.battery.voltage() < 6.5:
robot.led2.set(1)
print("[MOBOT]", 'LOW BATTERY - please change my batteries')
else:
robot.led2.set(0)
time.sleep(2)
  • Make the wheels turn: add a Set Motor … to … with speed … % block above the repeat forever block (see Speed ramp), send the program again, and watch the number while they turn. Does it change?
  • Use the map values block (see Joystick controls the speed) to turn 6.0-9.0 volts into 0-100, and show the battery as the brightness of LED 1: a battery gauge made of one light. Those are the robot’s own end points for empty and full.

The first Smart Cars, with the L298N motor chip, have no battery sensor. On one of those, on firmware 3.0, the serial monitor shows the first line and then stops with:

AttributeError: 'NoneType' object has no attribute 'voltage'

That robot has nothing to measure the battery with, so this example is for the standard Smart Car.