pdf-icon

GPIO

Refer to the Arduino API of Arduino

M5STACK Pin Arrangement:

Pin Number Name Allocation
0 G0 Downloader
1 T1 UART
2 G2 Side terminal (except M5FIRE), M5-BUS
3 R1 UART
4 G4 TF
5 G5 Side terminal (except M5FIRE), M5-BUS
6 G6 SDIO
7 G7 SDIO
8 G8 SDIO
9 G9 SDIO
10 G10 SDIO
11 G11 SDIO
12 G12 IIS_SCLK
13 G13 IIS_WS
14 G14 LCD
15 G15 IIS_OUT
16 R2 UART
17 T2 UART
18 G18 TF, Top terminal (SCK), M5-BUS
19 G19 TF, Top terminal (MISO), M5-BUS
21 G21 GROVE-A (SDA)
22 G22 GROVE-A (SCL)
23 G23 TF, Top terminal (MOSI)
25 G25 Speaker, Side terminal (except M5FIRE), M5-BUS
26 G26 Side terminal (except M5FIRE), M5-BUS
27 G27 LCD
32 G32 LCD BackLight
33 G33 LCD
34 G34 None
35 G35 Side terminal (except M5FIRE)
36 G36 Side terminal (except M5FIRE)
37 G37 Button C
38 G38 Button B
39 G39 Button A

pinMode

Function:

Set pin input/output mode.

Function Prototype:

void pinMode(uint8_t pin, uint8_t mode);

Parameter Type Description
pin uint8 Pin number
mode uint8 One of INPUT, OUTPUT, INPUT_PULLUP
Note:
If there are other circuits on the bus, be aware of the risk of physical damage. For instance, when the speaker (G25) terminal is set to INPUT mode, the current to the speaker causes the main unit to heat

up. Ensure correct operation as there is a risk of damage.

Example:

#include <M5Stack.h>

void setup() {
    M5.begin();
    M5.Power.begin();
    pinMode(16, OUTPUT);
}
void loop() {
}

digitalRead

Function:

Read the state of a pin.

Function Prototype:

int digitalRead(uint8_t pin);

Return Value:

Pin input state (0/1)

Note:
1. If the pin is not connected to anything, the returned value will be indeterminate.
2. If there are other circuits in the control line, they may be affected.

Example:

#include <M5Stack.h>

void setup() {
    M5.begin();
    M5.Power.begin();
    M5.Lcd.setTextSize(2);
}
void loop() {
    M5.Lcd.printf("Read value:%d\n", digitalRead(39));
    delay(500);
}

digitalWrite

Function:

Write a value to a pin.

Function Prototype:

void digitalWrite(uint8_t pin, uint8_t val);

Parameter Type Description
pin uint8 Pin number
val uint8 Output state (0/1)
Note:
1. If the pin mode is not set to OUTPUT, it will not return the correct result.
2. Be aware that if there are other circuits in the control line, physical damage could occur.

Example:

#include <M5Stack.h>

void setup() {
    M5.begin();
    M5.Power.begin();
    M5.Lcd.setTextSize(2);
    pinMode(16, OUTPUT);
}
void loop() {
    M5.update();
    M5.Lcd.printf("Read value:%d\n", digitalRead(16));
    delay(500);
}

analogRead

Function:

Read the value of an analog pin.

Function Prototype:

uint16_t analogRead(uint8_t pin);

Parameter Type Description
pin uint8 Pin number

Return Value:

The read result's maximum value is determined by analogSetWidth().

Example:

#include <M5Stack.h>

void setup() {
    M5.begin();
    M5.Power.begin();
    M5.Lcd.setTextSize(2);
}
void loop() {
    M5.Lcd.printf("Read value:%d\n", analogRead(35));
    delay(500);
}

dacWrite

Function:

DAC output to specified pin.

Function Prototype:

void dacWrite(uint8_t pin, uint8_t value);

Parameter Type Description
pin uint8 Pin number
value uint8 Set output voltage
Note:
1. Set the pin mode to OUTPUT before use.
2. G25 and G26 are available for M5Stack.

Example:

#include <M5Stack.h>

void setup() {
    M5.begin();
    M5.Power.begin();
    M5.Lcd.setTextSize(2);
    pinMode(25, OUTPUT);
}
void loop() {
    dacWrite(25,0x40);
    delay(500);
}

ledcSetup

Function:

Set duty cycle output.

Function Prototype:

double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits);

Parameter Type Description
channel uint8 Channel (0~15)
freq double Frequency (Hz)
resolution_bits uint8_t Full-scale bits of duty cycle

Return Value:

Actual output frequency

Note:
The channel and GPIO port number are not the same. It should be identified as a number for storing settings.

ledcAttachPin

Function:

Bind pin to specified ledc channel.

Function Prototype:

void ledcAttachPin(uint8_t pin, uint8_t channel);

Parameter Type Description
pin uint8_t Pin number
channel uint8_t Channel (0~15)

ledcWrite

Function:

Output with specified duty cycle value.

Function Prototype:

void ledcWrite(uint8_t channel, uint32_t duty);

Parameter Type Description
channel uint

8_t | Channel (0~15) | | duty | uint32_t | Duty |

Note:
The duty cycle depends on the bit ratio set during initialization. For example, specifying 0xFF for 8-bit will result in 100% output.

Example:

#include <M5Stack.h>

uint8_t pin = 25;  // Set output pin number

void setup() {
    M5.begin();
    M5.Power.begin();
    double f = ledcSetup(1, 1000, 10);  // Use ledc channel 1, set the frequency to 1kHz and the resolution to 10 bits
    Serial.printf("F=%.0f\n",f);  // print the actual setting frequency
    ledcAttachPin(pin,1);  // Bind the pin to ledc channel 1
    ledcWrite(1, 511);  // Set the duty cycle of ledc channel to 512/1024=50%
}

void loop() {
    delay(100);
}

ledcDetachPin

Function:

Release the specified port and stop output.

Function Prototype:

void ledcDetachPin(uint8_t pin);

Parameter Type Description
pin uint8_t Pin number

Example:

#include <M5Stack.h>

uint8_t pin = 25;  // Set output pin number

void setup() {
    M5.begin();
    M5.Power.begin();
    double f = ledcSetup(1, 1000, 10);  // Use ledc channel 1, set the frequency to 1kHz and the resolution to 10 bits
    Serial.printf("F=%.0f\n",f);  // print the actual setting frequency
    ledcAttachPin(pin,1);  // Bind the pin to ledc channel 1
    ledcWrite(1, 511);  // Set the duty cycle of ledc channel to 512/1024=50%
    ledcDetachPin(pin);  // Stop the pin output
}

void loop() {
    delay(100);
}

ledcRead

Function:

Return the duty cycle of the specified channel.

Function Prototype:

uint32_t ledcRead(uint8_t channel)

Parameter Type Description
channel uint8_t Specified channel

Return Value:

Duty cycle

Example:

#include <M5Stack.h>

uint8_t pin = 25;  // Set output pin number

void setup() {
    M5.begin();
    M5.Power.begin();
    double f = ledcSetup(1, 1000, 10);  // Use ledc channel 1, set the frequency to 1kHz and the resolution to 10 bits
    Serial.printf("F=%.0f\n",f);  // print the actual setting frequency
    ledcAttachPin(pin,1);  // Bind the pin to ledc channel 1
    ledcWrite(1, 511);  // Set the duty cycle of ledc channel to 512/1024=50%
    M5.Lcd.printf("ledcRead:%d\n", ledcRead(1));  // print the actual duty cycle
}

void loop() {
    delay(100);
}

ledcReadFreq

Function:

Return the frequency of the current channel.

Function Prototype:

double ledcReadFreq(uint8_t channel)

Parameter Type Description
channel uint8_t Specified channel

Return Value:

Channel frequency

Example:

#include <M5Stack.h>

uint8_t pin = 25;  // Set output pin number

void setup() {
    M5.begin();
    M5.Power.begin();
    double f = ledcSetup(1, 1000, 10);  // Use ledc channel 1, set the frequency to 1kHz and the resolution to 10 bits
    Serial.printf("F=%.0f\n",f);  // print the actual setting frequency
    ledcAttach Pin(pin,1);  // Bind the pin to ledc channel 1
    ledcWrite(1, 511);  // Set the duty cycle of ledc channel to 512/1024=50%
    M5.Lcd.printf("ledcRead:%d\n", ledcRead(1));  // print the actual duty cycle
}

void loop() {
    delay(1000);
    M5.Lcd.printf("ledcReadFreq:%.3f\n",ledcRead

Freq(1));  // print the actual duty cycle
}

adcAttachPin

Function:

In non-blocking mode, connect the designated pin to ADC.

Function Prototype:

bool adcAttachPin(uint8_t pin)

Parameter Type Description
pin uint8_t Specified pin

Example:

#include <M5Stack.h>

uint8_t pin = 25;  // Set output pin number

void setup() {
    M5.begin();
    M5.Power.begin();
    if (adcAttachPin(pin)) {  // Attach the pin to ADC
        M5.Lcd.printf("Pin %d attached to ADC", pin);
    };
}

void loop() {}

adcStart

Function:

In non-blocking mode, start ADC conversion.

Function Prototype:

bool adcStart(uint8_t pin)

Parameter Type Description
pin uint8_t Specified pin

Return Value:

Returns 1 if start successfully, otherwise returns 0.

adcBusy

Function:

In non-blocking mode, check if ADC conversion is in progress.

Function Prototype:

bool adcBusy(uint8_t pin)

Parameter Type Description
pin uint8_t Specified pin

adcEnd

Function:

In non-blocking mode, return the conversion result, if not converted wait until completion.

Function Prototype:

uint16_t adcEnd(uint8_t pin)

Parameter Type Description
pin uint8_t Specified pin

Return Value:

Returns the conversion result.

analogReadResolution

Function:

Set the resolution of analog data reading, values 1~16, default is 12. If between 9 and 12, it equals the set hardware resolution, otherwise, the value will be shifted.

Function Prototype:

void analogReadResolution(uint8_t bits)

Parameter Type Description
bits uint8_t Sampling resolution

analogSetAttenuation

Function:

Set global input attenuation for ADC, values ADC_0db, ADC_2_5db, ADC_6db, ADC_11db, default is 11db.

Function Prototype:

void analogSetAttenuation(adc_attenuation_t attenuation)

Parameter Type Description
attenuation adc_attenuation_t Attenuation value

analogSetCycles

Function:

Set sampling cycles 1-255, default is 8.

Function Prototype:

void analogSetCycles(uint8_t cycles)

Parameter Type Description
cycles uint8_t Sampling period

analogSetPinAttenuation

Function:

Set attenuation for an individual pin.

Function Prototype:

void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation)

Parameter Type Description
pin uint8_t Specified pin
attenuation adc_attenuation_t Attenuation value

analogSetSamples

Function:

Set the actual sampling multiplier for a single sample, values 1-255, default is 1, the actual effect is to improve the ADC sensitivity, the sampling number is expanded by N times.

Function Prototype:

void analogSetSamples(uint8_t samples)

Parameter Type Description
samples uint8_t Sampling multiplier

analogSetWidth

Function:

Set ADC sampling resolution 9-12, default is 12.

Function Prototype:

void analogSetWidth(uint8_t bits)

Parameter Type Description
bits uint8_t Sampling resolution

attachInterrupt

Function:

Set pin interrupt.

Function Prototype:

void attachInterrupt(pin, ISR(callback function), interrupt type/mode)

Parameter Type Description
pin uint8_t Pin
ISR callback function Callback function
interrupt mode Trigger type

detachInterrupt

Function:

Disable specified pin interrupt.

Function Prototype:

void detachInterrupt(pin)

Parameter Type Description
pin uint8_t Pin number.

hallRead

**Function

:**

Read the Hall sensor.

Function Prototype:

int hallRead(void));

On This Page