🛠 Mastering GPIO and Display on ToughC5
Objectives
This tutorial guides you through manipulating GPIO (General Purpose Input/Output) pins and using the integrated color LCD screen. You will learn to control a logical pin and display dynamic text, essential foundations for any IoT project with the ToughC5.
Required Hardware
- M5Stack ToughC5 IoT Dev Kit
- USB Type-C cable for power and programming
- Computer with Arduino IDE installed
- M5Unified library (via Arduino Library Manager)
Steps
Open the Arduino IDE. Go to File > Preferences and add the M5Stack Board Manager URL if not already done. Install the M5Stack board via the Board Manager.
- Environment Setup
In Sketch > Include Library > Manage Libraries, search for M5Unified and install the latest stable version. This library simplifies hardware access, including the ILI9342C screen and GPIOs.
- Library Installation
Create a new sketch and paste the following code to initialize the system and screen: ```cpp #include <M5Unified.h>
- Basic Sketch Creation
void setup() { M5.begin(); M5.Display.init(); M5.Display.fillScreen(BLACK); M5.Display.setTextColor(WHITE); M5.Display.setCursor(10, 10); M5.Display.println("ToughC5 GPIO Test"); }
void loop() { M5.update(); } ```
The ToughC5 exposes several HY2.0-4P connectors. For this example, we will use a generic GPIO pin. Note that specific pins depend on the connector used (I2C/GPIO/UART). To simulate an output, we will flash a visual indicator on the screen rather than an external LED to keep it simple. Modify the loop() function: ```cpp void loop() { M5.update(); static bool state = false; state = !state;
- GPIO Control
if (state) { M5.Display.fillCircle(160, 120, 20, GREEN); } else { M5.Display.fillCircle(160, 120, 20, RED); } delay(500); } ```
Select the corresponding serial port for your ToughC5 in Tools > Port. Click the « Upload » button to flash the program.
- Upload
Verification
Once the upload is complete, the screen should display the title « ToughC5 GPIO Test » at the top. A circle should alternate between green and red in the center of the screen every 500 milliseconds. If the screen remains black, check that the USB cable is properly connected and that the selected port is correct. Ensure the screen brightness is sufficient (the ToughC5 has a very bright 853-nit screen).
Further Exploration
Explore digital inputs by connecting a push button to an HY2.0 connector configured as GPIO. Use M5.In_I2C or native GPIO functions to read the button state and change the screen background color. You can also try using the integrated passive buzzer to add audio feedback synchronized with the visual blinking.
Other tutorials for this board
Code examples for this board
toughc5_gpio_button_buzzer.inoC++ (Arduino)toughc5_wifi6_scanner.inoC++ (Arduino)toughc5_rtc_battery_log.inoC++ (Arduino)toughc5_ili9342c_touch_ui.inoC++ (Arduino)toughc5_rs485_modbus_master.inoC++ (Arduino)toughc5-rs485-modbus-master.inoArduino C++