*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 |
Function:
Set pin input/output mode.
Syntax:
void pinMode(uint8_t pin, uint8_t mode);
Parameter | Type | Description |
---|---|---|
pin | uint8 | Pin number |
mode | uint8 | One of INPUT, OUTPUT, INPUT_PULLUP |
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() {
}
Function:
Read the state of a pin.
Syntax:
int digitalRead(uint8_t pin);
Return Value:
Pin input state (0/1)
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);
}
Function:
Write a value to a pin.
Syntax:
void digitalWrite(uint8_t pin, uint8_t val);
Parameter | Type | Description |
---|---|---|
pin | uint8 | Pin number |
val | uint8 | Output state (0/1) |
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);
}
Function:
Read the value of an analog pin.
Syntax:
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);
}
Function:
DAC output to specified pin.
Syntax:
void dacWrite(uint8_t pin, uint8_t value);
Parameter | Type | Description |
---|---|---|
pin | uint8 | Pin number |
value | uint8 | Set output voltage |
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);
}
Function:
Set duty cycle output.
Syntax:
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
Function:
Bind pin to specified ledc channel.
Syntax:
void ledcAttachPin(uint8_t pin, uint8_t channel);
Parameter | Type | Description |
---|---|---|
pin | uint8_t | Pin number |
channel | uint8_t | Channel (0~15) |
Function:
Output with specified duty cycle value.
Syntax:
void ledcWrite(uint8_t channel, uint32_t duty);
Parameter | Type | Description |
---|---|---|
channel | uint |
8_t | Channel (0~15) | | duty | uint32_t | Duty |
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);
}
Function:
Release the specified port and stop output.
Syntax:
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);
}
Function:
Return the duty cycle of the specified channel.
Syntax:
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);
}
Function:
Return the frequency of the current channel.
Syntax:
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
}
Function:
In non-blocking mode, connect the designated pin to ADC.
Syntax:
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() {}
Function:
In non-blocking mode, start ADC conversion.
Syntax:
bool adcStart(uint8_t pin)
Parameter | Type | Description |
---|---|---|
pin | uint8_t | Specified pin |
Return Value:
Returns 1 if start successfully, otherwise returns 0.
Function:
In non-blocking mode, check if ADC conversion is in progress.
Syntax:
bool adcBusy(uint8_t pin)
Parameter | Type | Description |
---|---|---|
pin | uint8_t | Specified pin |
Function:
In non-blocking mode, return the conversion result, if not converted wait until completion.
Syntax:
uint16_t adcEnd(uint8_t pin)
Parameter | Type | Description |
---|---|---|
pin | uint8_t | Specified pin |
Return Value:
Returns the conversion result.
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.
Syntax:
void analogReadResolution(uint8_t bits)
Parameter | Type | Description |
---|---|---|
bits | uint8_t | Sampling resolution |
Function:
Set global input attenuation for ADC, values ADC_0db, ADC_2_5db, ADC_6db, ADC_11db, default is 11db.
Syntax:
void analogSetAttenuation(adc_attenuation_t attenuation)
Parameter | Type | Description |
---|---|---|
attenuation | adc_attenuation_t | Attenuation value |
Function:
Set sampling cycles 1-255, default is 8.
Syntax:
void analogSetCycles(uint8_t cycles)
Parameter | Type | Description |
---|---|---|
cycles | uint8_t | Sampling period |
Function:
Set attenuation for an individual pin.
Syntax:
void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation)
Parameter | Type | Description |
---|---|---|
pin | uint8_t | Specified pin |
attenuation | adc_attenuation_t | Attenuation value |
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.
Syntax:
void analogSetSamples(uint8_t samples)
Parameter | Type | Description |
---|---|---|
samples | uint8_t | Sampling multiplier |
Function:
Set ADC sampling resolution 9-12, default is 12.
Syntax:
void analogSetWidth(uint8_t bits)
Parameter | Type | Description |
---|---|---|
bits | uint8_t | Sampling resolution |
Function:
Set pin interrupt.
Syntax:
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 |
Function:
Disable specified pin interrupt.
Syntax:
void detachInterrupt(pin)
Parameter | Type | Description |
---|---|---|
pin | uint8_t | Pin number. |
**Function
:**
Read the Hall sensor.
Syntax:
int hallRead(void));