Are you looking to add some external components to your next embedded system project? Well, look no further than GPIO pins! General purpose input output or just GPIO for short: these versatile pins are a key part of many microcontrollers. They allow you to control LEDs, read input from buttons and sensors, and even drive motors. In this Hackster article, we'll show you how to work with GPIO pins using Micropython on the Cypress PSOC6 board, a powerful microcontroller that packs a punch when it comes to features and capabilities. Whether you're a seasoned embedded systems developer or just starting out, this article will provide you the tools and knowledge you need to get started using GPIO pins in your project. So let's dive in and see what we can create with the power of GPIO pins!
Setting up the PSOC6 board for MicropythonBefore using the GPIO Ports, it is necessary to make sure that Micropython is installed on your PSoC6 board. Follow the instructions provided in the "Micropython on PSoC6" Protip. This comprehensive guide will walk you through installing Micropython on the PSoC6 and demonstrate how to operate its onboard LED.
GPIOs in a nutshellGPIOs are universal pins that can either sense a digital signal or deliver it.
In electronics, a digital signal is a binary signal that can only have two discrete states: 1 or 0. In other words, a digital signal can either be HIGH or LOW, where LOW is equivalent to 0 or ground and HIGH is equivalent to 1 or VDD (which depends on the microcontroller used in the circuit).
Working with GPIO pins1. Import the machine
module:
import machine
The machine
module is a core component of MicroPython that provides functionalities to interact directly with the hardware components of a microcontroller. By leveraging the functionalities offered by this module, developers can gain direct control over the I/O pins, pulse width modulation (PWM), I2C, SPI, UART communication peripherals, and more.
Alternatively, you could just import the Pin module, which is the core module for GPIO control using:
from machine import Pin
2. Set up a GPIO pin:
Ourpin = machine.Pin(pin_number, mode, pull=None, value=0)
pin_number
is the GPIO pin number you want to use. This can be an integer or a string.
mode
is the mode you want to set for the pin. This can be:
machine.Pin.IN
for an input pin.machine.Pin.OUT
for an output pin.machine.Pin.OPEN_DRAIN
for an open-drain pin.
In open-drain mode, the pin can only sink current (i.e., pull the output low) but cannot source current (i.e., pull the output high).
machine.Pin.ALT
for an alternative pin function.
Alternative function modes refer to the ability of certain pins on microcontrollers or other embedded systems to serve multiple purposes. In addition to their basic input/output functionality, these pins can be configured to perform alternative functions such as serial communication (UART), pulse-width modulation (PWM), I2C, SPI, or other specialized tasks. For example, a GPIO pin on a microcontroller might default to being a general-purpose digital input or output, but you can configure it to function as a UART transmit or receive pin by setting the alternative function mode accordingly. This flexibility allows a single physical pin to serve different purposes based on the application’s requirements, optimizing the use of limited resources on the hardware.
pull
is the pull-up or pull-down resistor state. This can be:
None
for no pull-up or pull-down resistor.
This option means that no pull-up or pull-down resistor is connected to the pin. In this case, the state of the pin will be undefined when it is not being actively driven by some other source. This is sometimes referred to as a "floating" state.
machine.Pin.PULL_UP
for a pull-up resistor.
This option connects a pull-up resistor to the pin. A pull-up resistor is designed to pull the pin to a high voltage level (usually equal to the supply voltage) when it is not actively being driven low by some other source. This is a common configuration for buttons or switches, where the pin is normally high, and is pulled low when the button is pressed.
machine.Pin.PULL_DOWN
for a pull-down resistor.
This option connects a pull-down resistor to the pin. A pull-down resistor is designed to pull the pin to a low voltage level (usually ground) when it is not actively being driven high by some other source. This configuration is less common than pull-up resistors, but is useful in some cases, such as when interfacing with certain types of sensors.
value
is the initial value for the pin. For an output pin, this can be 0 or 1. However, for an input pin, this is ignored.
3.Read or write to the GPIO pin:
Ourpin.value()
- This returns the current value of the pin (0 or 1).
Ourpin.value(0) or Ourpin.value(1)
- If the pin is set to output, you could also insert a 0 or 1 into the value() function to turn the GPIO low or high respectively.
LEDs are one of those devices that are very easy to talk to. They don't expect that much information from your requests, like baud rate, protocols, or operating frequencies. All they need is just for you to tell them to turn on or off. Luckily for you, the PSoC6 comes with a built-in LED connected to pin P13_7, so without the need for any external connections let's demonstrate how we could control this LED.
Step 1: Importing the modules
As mentioned in the previous section, the first thing to always do is to import our modules.
from machine import Pin
Step 2: Variable Creation
LEDpin=Pin("P13_7", Pin.OUT, pull=None, value=1)
Step 3: Control
To toggle the LED:
LEDpin.toggle()
To turn it off:
LEDpin.off()
or
LEDpin.value(0)
To turn it on:
LEDpin.on()
or
LEDpin.value(1)
This article aims to guide you to the world of GPIO pins and how they can be used to control input and output signals in microcontroller projects. We've walked through how to get started with GPIO ports using Micropython on the PSoC6 board and even had a chance to see how to control LEDs with these pins. Don't forget to tune in for more guides and keep an eye open for our cool projects @ https://www.hackster.io/Infineon_Team.
In addition, following you find the differences between GPIOs, PWM, and ADCs and learn how each one can be used to achieve specific signal requirements within your projects.
GPIOs vs PWMIn the previous section, we discussed how GPIO pins can only handle binary signals of HIGH and LOW. However, for more complex signal requirements, a Pulse Width Modulation (PWM) signal can be used to achieve a custom voltage signal. In this technique, the microcontroller generates bursts of square wave signals with different widths, which when averaged per unit time, can generate any voltage between the microcontroller's VDD and ground.
While PWM is a powerful and versatile technique, it requires special hardware and is not generally available on all pins. Therefore, it is important to carefully consider your pin connections whenever you intend to use PWM in your project setup.
Note: On the PSoC6, all output-capable pins are PWM-capable.GPIOs vs ADC
It is not just important to generate custom voltage signals but also to sense and measure them accurately. This is where the Analog-to-Digital Converter (ADC) comes in handy. Unlike GPIO pins which can only handle digital signals, an Analog-to-Digital (ADC) converter can convert analog signals into digital signals that can be easily processed by the microcontroller.
The ADC works by dissecting the analog signal at regular intervals and comparing it to known reference values to determine its precise voltage level. This conversion process enables the accurate measurement of analog signals and allows for a wide range of applications, from environmental sensing to control systems.
Note: PSoC6 supports only one 12-bit SAR ADC with the following channel-to-pin mapping and the defaults are set accordingly: To learn more about using the ADC on the PSoC6, check out the "Step by Step guide to using ADC on the PSoC6" Protip, which provides detailed instructions and code examples for measuring analog signals.
Comments
Please log in or sign up to comment.