Skip to content

Search the documentation

Kit
Firmware

Read a pin — Smart Car

Read Pin … reads one input pin and gives you its value: 0 or 1. This program prints the value of pin OUT1 about three times a second, so you can cover the sensor on that pin with a finger and watch the number change.

The five pins in the list of Read Pin, OUT1 to OUT5, are the pins of the five line sensors in a row underneath the robot. OUT1 is the leftmost of them.

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 "Reading pin OUT1 about three times a second."
  • repeat forever
    • Print to serial (create text with "OUT1 = " Read Pin OUT1)
    • wait 0.3 seconds
  • A Smart Car with the line-sensor array fitted underneath.
  • A USB cable.
  1. Open 13-read-pin.mobot, plug in, press Send to Device.
  2. Watch the serial monitor.
  3. Slide a finger under the OUT1 sensor, then take it away again.
  • [MOBOT] Reading pin OUT1 about three times a second. appears once.
  • Then a new line about three times a second: OUT1 = 0 or OUT1 = 1.
  • With a finger under the sensor the number changes, and when you take the finger away it changes back.
  • A line sensor over a black line reads 1, and over a white floor it reads 0.
  • It never stops. Press Stop, or send a different program, to end it.

It works if the number changes as you cover and uncover the sensor.

Read Pin list pin (GPIO) the same sensor in the Read IR block
OUT1 36 OUT1
OUT2 39 OUT2
OUT3 34 OUT3
OUT4 35 OUT4
OUT5 32 OUT5

Read IR, in the Sensors group, reads these same five sensors under the same names, OUT1 to OUT5, and gives the same 0 or 1. It is the friendlier way to read a line sensor: it says what it reads, it sits with the other sensor blocks, and it has a partner, compare IRs, that checks all five sensors at once. Follow the line is built on it. Read Pin is for the times you want to read a pin directly.

block what it does
mobot_readpin Read Pin …: the value of one input pin, 0 or 1.
mobot_print Print to serial: writes a line in the serial monitor. Here it joins the words OUT1 = and the number.
repeat_forever, wait read and print over and over, with a pause of 0.3 seconds.

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. For these blocks the two tabs hold the same program.

13-read-pin.firmware-3.main.py
# Read a pin — robotics (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 13-read-pin.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 13-read-pin.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Reading pin OUT1 about three times a second.')
while True:
print("[MOBOT]", ('OUT1 = ' + str(Pin(36, Pin.IN).value())))
time.sleep(0.3)
  • Pick OUT3 in the Read Pin block, and change the words OUT1 = to match. Now the middle sensor is the one you cover.
  • Swap Read Pin OUT1 for Read IR OUT1. The numbers you see are the same.
  • Add a second Print to serial block that reads OUT2, and watch two sensors at once.
  • Change the wait to 1: one line a second. Change it to 0.05: the lines come much faster.