<aside> đź“Ž Overview: In this page, we will delve into the capabilities of microcontrollers and learn about their interactions with various peripherals. You will tinker with CYOBrain using the given sample code and observe how it interfaces with peripherals such as LED matrices, buttons, motors, micro SD cards, speakers, microphones, IMUs (Inertial Measurement Units), and connect to the internet.
</aside>
Microcontrollers often have similar configurations. A typical microcontroller includes a processor, memory and input/output (I/O) peripherals on a single chip.
Sample microcontroller development board using ESP32 and its peripherals
CYOBrain
CYOBrain, CYOBot's microcontroller development board, uses a microcontroller called ESP32, and comes with multiple peripherals common in robotic applications: RGB LEDs, buttons, Inertial Measurement Unit (IMU), motor driver, microphone, speaker and micro SD card reader.
Run the sample code below (by copying the following code, paste it to the Code Editor of the portal, press Run and observe the robot). What do you see?
from lib.brain.display import *
matrix = Matrix()
matrix.scroll("HELLO WORLD", red=0, green=0, blue=100, speed=0.1)
Run the sample code below. An LED at the center of the robot should light up. Sequentially press the left and right button, what do you see when the button is pressed?
import machine, neopixel
import time
np = neopixel.NeoPixel(machine.Pin(33), 23)
left = machine.Pin(27, machine.Pin.IN)
right = machine.Pin(0, machine.Pin.IN)
led_index = 4
led_range = [12, 13, 14, 15, 16, 17, 18, 19, 20]
while True:
# clear the screen every time
for index in led_range:
np[index] = (0, 0, 0)
# turn the correct LED on
np[led_range[led_index]] = (0, 0, 100)
np.write()
# check value of buttons to update index for next run
if left.value() == 0:
led_index = max(0, led_index - 1)
elif right.value() == 0:
led_index = min(8, led_index + 1)
time.sleep(0.01)
<aside> ⚠️ Once finished, press the Break button at the bottom right of the portal to terminate the code before continuing with other activities.
</aside>
Follow the video to remove the SD card from the robot, plug the card into your computer and open the card.
Run sample code below. Do you see a file called create_a_file.txt? Double click on the file to open it, do you see the content inside the file? Compare this with the code you just run, are they the same?
with open("/sd/create_a_file.txt", "w") as file:
file.write("Look at this! You can read and write files into SD card!")