IO
In this section, you will learn about IO pins, its purpose, and how you can use them for your projects.
Introduction
The Echo features a versatile array of GPIO (General Purpose Input/Output) pins, enabling control over various sensors and devices. These pins support a wide range of communication signals, including:
Digital: For on/off states
PWM (Pulse Width Modulation): For analog-like control using digital signals
I2C (Inter-Integrated Circuit): A two-wire serial communication protocol
UART (Universal Asynchronous Receiver-Transmitter): For serial data transmission
SPI (Serial Peripheral Interface): A synchronous serial data protocol
Analog: For reading continuous voltage values
A key advantage of the Echo's pins is their full configurability. This means that most pins can serve various purposes, provided they are correctly declared and configured in your code.
Important Note on PWM: Due to the Echo's integrated motor drivers utilizing all of the ESP32's built-in MCPWM (Motor Control PWM) channels, additional MCPWM functionality may not be available for other GPIO purposes if the motor drivers are actively used and declared in your code.
Given the extensive number of communication protocols and compatible devices, we will provide fundamental examples for each communication type to demonstrate the Echo's versatility.
Digital I/O
Digital communication, utilizing digitalWrite() and digitalRead() functions in Arduino, involves setting a pin to a HIGH (typically 3.3V) or LOW (0V) state for output, or detecting a HIGH or LOW state for input. This allows for basic on/off control or sensing of binary signals.
Digital Input Example
For this example, we will demonstrate reading data from a digital sensor, specifically the MAGsense sensor by 3DBU.
#define HALL_SENSOR_PIN 1 // Set the pin accordingly, This must be set on a DIGITAL enabled pin
void setup() {
pinMode(HALL_SENSOR_PIN, INPUT);
Serial.begin(115200);
}
void loop() {
int sensorState = digitalRead(HALL_SENSOR_PIN);
if (sensorState == HIGH) {
Serial.println("Magnet Detected");
} else {
Serial.println("No Magnet");
}
delay(500); // Delay for readability, adjust as needed
}Digital Output Example
This example demonstrates controlling an LED or relay using digital output.
The code above configures digital pins for both input and output, demonstrating basic on/off control and binary signal detection.
PWM (Pulse Width Modulation)
PWM allows you to simulate analog output by rapidly switching a digital pin between HIGH and LOW states. This is useful for controlling LED brightness, servo positions, or motor speeds.
This example reads an analog input and uses it to control PWM output, demonstrating variable analog-like control using digital signals.
I2C Communication
I2C (Inter-Integrated Circuit) is a powerful and efficient two-wire serial communication protocol. It's particularly useful because it allows for daisy-chaining multiple devices (provided they have unique I2C addresses), significantly reducing the number of pins required for communication. It is also known for its speed and ease of use.
I2C Scanner
The Echo has one available I2C channel. This example demonstrates how to use the Echo to scan for and identify I2C devices connected to its I2C bus. This is an invaluable tool for troubleshooting and confirming that your I2C sensors or modules are properly connected and recognized.
I2C Device Communication
This example demonstrates reading data from a common I2C temperature sensor (like the BME280):
UART Communication
UART provides asynchronous serial communication, useful for connecting to GPS modules, other microcontrollers, or serial devices.
This example demonstrates bidirectional UART communication with a GPS module, showing both data reception and command transmission.
SPI Communication
SPI is a high-speed synchronous communication protocol commonly used with displays, SD cards, and high-speed sensors.
This example shows SPI configuration and basic read operation, demonstrating the protocol's chip-select mechanism and transaction management.
Analog Input
Analog pins can read continuous voltage values, useful for sensors that output variable voltages like potentiometers, light sensors, or temperature sensors.
This example demonstrates reading analog values and converting them to meaningful measurements.
Conclusion
The examples provided here illustrate the fundamental versatility of the Echo's I/O pins. While these specific sensors are used for demonstration, the principles apply to a vast array of compatible devices and communication protocols. Ultimately, the choice and configuration of your I/O pins will be dictated by the specific requirements and integrated components of your project.
Last updated