🛠 Mastering GPIO and Display on ToughC5

M5Stack ToughC5 Weatherproof IoT Dev Kit (ESP32-C5) · debutant · Apprenez à contrôler les broches d'entrée/sortie et à afficher des données sur l'écran tactile du M5Stack 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

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.

  1. 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.

  1. Library Installation

Create a new sketch and paste the following code to initialize the system and screen: ```cpp #include <M5Unified.h>

  1. 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;

  1. 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.

  1. 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

← Back to the board 🛠 All tutorials