Talk to a pin directly — Smart Car
The LEDs, the buzzer and the motor driver of the Smart Car are connected to pins. The blocks you have used so far, like Set LED, look after the pin for you. These two blocks let you switch a pin yourself: Set Pin … to HIGH / LOW switches it all the way on or all the way off, and Set PWM of Pin … switches it on and off 100 times a second and decides how much of each round it stays on, so you get anything between off and fully on.
The pin called LED1 in the list of pins is the light that the Set LED blocks call LED 1, and LED2 is LED 2.
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 program
Section titled “The program”Read the program as text
- MoBot Start
- Print to serial
"Pin blocks. Watch both LEDs." - wait 2 seconds
- repeat forever
- Print to serial
"Set Pin LED1 to HIGH" - Set Pin LED1 to HIGH
- wait 1.5 seconds
- Print to serial
"Set Pin LED1 to LOW" - Set Pin LED1 to LOW
- wait 1 seconds
- Print to serial
"Set Pin LED2 to HIGH" - Set Pin LED2 to HIGH
- wait 1.5 seconds
- Print to serial
"Set Pin LED2 to LOW" - Set Pin LED2 to LOW
- wait 1 seconds
- Print to serial
"PWM on LED1: a quarter, a half, full" - Set PWM of Pin LED1 to 256 (0 - 1023)
- wait 1 seconds
- Set PWM of Pin LED1 to 512 (0 - 1023)
- wait 1 seconds
- Set PWM of Pin LED1 to 1023 (0 - 1023)
- wait 1 seconds
- Set PWM of Pin LED1 to 0 (0 - 1023)
- wait 1.5 seconds
- Print to serial
What you need
Section titled “What you need”- A Smart Car. Nothing to plug in: both LEDs are on the board.
- A USB cable.
- A dim room helps for the PWM part.
How to run it
Section titled “How to run it”- Open 12-pin-output.mobot, plug in, press Send to Device.
- Put the robot where you can see both LEDs at once.
- Read each line in the serial monitor and look at the LEDs at the same time.
What you should see
Section titled “What you should see”[MOBOT] Pin blocks. Watch both LEDs. appears once. After a pause of 2 seconds
the same cycle runs over and over. One cycle takes about 9 and a half seconds.
| serial line | what happens |
|---|---|
Set Pin LED1 to HIGH |
LED 1 comes on, at full brightness |
Set Pin LED1 to LOW |
LED 1 goes out |
Set Pin LED2 to HIGH |
LED 2 comes on |
Set Pin LED2 to LOW |
LED 2 goes out |
PWM on LED1: a quarter, a half, full |
LED 1 comes on dim, then brighter, then at full brightness, and then goes out |
It works if each pin lights its own LED, and if LED 1 shows three brightnesses during the PWM part, each brighter than the one before.
What the PWM numbers mean
Section titled “What the PWM numbers mean”Set PWM takes a number from 0 to 1023. The number says how much of the time the pin is on.
| number | the pin is on |
|---|---|
| 0 | never: the LED is off |
| 256 | about a quarter of the time |
| 512 | about half of the time |
| 1023 | all of the time: full brightness |
The friendly Set LED to … % block uses 0 to 100 instead. 100 % there is the same as 1023 here.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_setpins |
Set Pin … to …: a pin all the way HIGH (on) or all the way LOW (off). |
mobot_setpwm |
Set PWM of Pin … to …: a pin on for part of the time, 0 to 1023. A number outside that range is refused, and the program stops with an error. |
repeat_forever, wait, mobot_print |
the cycle, its pauses, and the lines in the serial monitor. |
Leave IN1, IN2, IN3 and IN4 alone unless the robot is standing on a book. Those four pins in the list run the wheels, so setting one of them to HIGH can make a wheel turn.
The Python code
Section titled “The Python code”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.
# Talk to a pin directly — robotics (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 12-pin-output.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 12-pin-output.main.py.
from machine import Pinimport timefrom machine import PWM
print("[MOBOT]", 'Pin blocks. Watch both LEDs.')time.sleep(2)while True: print("[MOBOT]", 'Set Pin LED1 to HIGH') PIN15 = Pin(15, Pin.OUT) PIN15.value(1) time.sleep(1.5) print("[MOBOT]", 'Set Pin LED1 to LOW') PIN15 = Pin(15, Pin.OUT) PIN15.value(0) time.sleep(1) print("[MOBOT]", 'Set Pin LED2 to HIGH') PIN2 = Pin(2, Pin.OUT) PIN2.value(1) time.sleep(1.5) print("[MOBOT]", 'Set Pin LED2 to LOW') PIN2 = Pin(2, Pin.OUT) PIN2.value(0) time.sleep(1) print("[MOBOT]", 'PWM on LED1: a quarter, a half, full') pwm_15 = PWM(Pin(15), freq=100, duty=0) pwm_15.duty(256) time.sleep(1) pwm_15 = PWM(Pin(15), freq=100, duty=0) pwm_15.duty(512) time.sleep(1) pwm_15 = PWM(Pin(15), freq=100, duty=0) pwm_15.duty(1023) time.sleep(1) pwm_15 = PWM(Pin(15), freq=100, duty=0) pwm_15.duty(0) time.sleep(1.5)# Talk to a pin directly — robotics## This is what MoBot puts on the board as main.py when you open# 12-pin-output.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport timefrom machine import PWM
print("[MOBOT]", 'Pin blocks. Watch both LEDs.')time.sleep(2)while True: print("[MOBOT]", 'Set Pin LED1 to HIGH') PIN15 = Pin(15, Pin.OUT) PIN15.value(1) time.sleep(1.5) print("[MOBOT]", 'Set Pin LED1 to LOW') PIN15 = Pin(15, Pin.OUT) PIN15.value(0) time.sleep(1) print("[MOBOT]", 'Set Pin LED2 to HIGH') PIN2 = Pin(2, Pin.OUT) PIN2.value(1) time.sleep(1.5) print("[MOBOT]", 'Set Pin LED2 to LOW') PIN2 = Pin(2, Pin.OUT) PIN2.value(0) time.sleep(1) print("[MOBOT]", 'PWM on LED1: a quarter, a half, full') pwm_15 = PWM(Pin(15), freq=100, duty=0) pwm_15.duty(256) time.sleep(1) pwm_15 = PWM(Pin(15), freq=100, duty=0) pwm_15.duty(512) time.sleep(1) pwm_15 = PWM(Pin(15), freq=100, duty=0) pwm_15.duty(1023) time.sleep(1) pwm_15 = PWM(Pin(15), freq=100, duty=0) pwm_15.duty(0) time.sleep(1.5)Try changing it
Section titled “Try changing it”- Pick LED2 in the Set PWM block, and watch the other LED fade instead.
- Try other numbers in Set PWM: 50, 100, 200, 800. Which ones can you tell apart?
- Make every
waitshorter and the cycle runs faster.