<aside> 📎 Overview: Understand the concept of GPIO and machine library. Put into practice to contextualize how to use these.

</aside>

Introduction

GPIO stands for General Purpose Input/Output. Components (parts of the robot) are connected to the microcontroller through GPIOs and the machine library provides code that helps you interface with these components easier. Detail information of the library can be found here: https://docs.micropython.org/en/latest/library/machine.html

Each GPIO is a physical pin that can drive an output voltage and read input voltages. Because of this, the direction can either be IN (read input voltage) or OUT (write output voltage). To interface with a component connected to a physical pin, we need to first indicate where it is connected, and the direction (IN or OUT). The syntax is:

from machine import Pin
pin = Pin(int, int)

With the first integer the index of the pin (see figure above to choose the correct number indicating where each component is connected to), and the second object defines the mode of the pin. For convenience, instead of having to remember which value is what mode, the machine.Pin class holds these values for you. Simply call either Pin.OUT or Pin.IN to access these values. Example:

from machine import Pin
pin = Pin(0, [Pin.IN](<http://pin.in/>))

This will let the microcontroller know that you are trying to control something connected to pin number 0, and that this thing is a type of input component.

Interface with buttons

Look at the figure, there are 2 buttons accessible on the robot, one connected to IO0 and another connected to IO27. Establish the connection with:

from machine import Pin
button0 = Pin(0, Pin.IN)
button1 = Pin(27, Pin.IN)

Read the value (state) of the button with:

Question: What is the value of a button when it is not pressed? How about when it is pressed?