Introduction
This I2C to IO expansion board based on CH423 chip extends 24 digital ports via I2C, including 8 GPIOs with interrupt functions and 16 GPOs. CH423 has a built-in current drive circuit that offers 15mA continuous drive current, which can statically drive 24 LED tubes or dynamically drive 128 LEDs.
This CH423 I2C to GPIO IO expander is suitable for projects involving LED tubes, dot matrix screen, light cube, etc.
Features
- 8 GPIO(general-purpose input and output) pins
- 16 GPO(general-purpose output) pins
- Built-in current drive circuit, continuous drive current of up to 15mA
Application
- Driving LED lights
- Driving Dot matrix screen
- Driving Light Cubes
Specification
- Supply Voltage:3.3V~5V
- Static Current: 1mA
- IO Continuous Drive Current: 15mA
- Communication Mode: I2C
- I2C Address: 0x20-0x3F (This module will occupy the I2C addresses of 0x20-0x3F and can't be used with other I2C devices in the address range.)
- Operating Temperature: -20℃-80℃
- Dimension: 45*37mm/1.77x1.46"
Pinout
NO. | Silkscreen | Description |
---|---|---|
1 | + | Power + |
2 | - | Power - |
3 | SCL | I2C clock line |
4 | SDA | I2C data line |
5 | GPIO | Input and output pin |
6 | GPO | Output pin |
Pin Mode
Pin | Configurable Mode | Default |
---|---|---|
GPO | Open-drain output and push-pull output | Push-pull output high level |
GPIO | Input and output | Pull-up input |
Note: The pins in the same group can only be set to the same mode at the same time, and the pins in different groups can be set to different modes.
Tutorial for Arduino
Requirements
- Hardware
- DFRduino UNO R3 (or similar) x 1
- Gravity: CH423 I2C 24 Digital IO Expansion Module x 1
- M-M/F-M/F-F Jumper wires
- Software
- Arduino IDE
- Download and install the CH432 Library (About how to install the library?)
Connection Diagram
Sample Code 1 - blink.ino
/*!
* @file blink.ino
* @brief Turns an LED on for one second, then off for one second, repeatedly.
* @note This module implements the blink function in 2 ways:
* @n 1. Set GPIO group pins to output mode and connect the LED lamp to one of the GPIO pins (GPIO0~GPIO7), and control the pin to output high and low levels;
* @n 2. Set GPO group pins as push-pull output mode and connect the LED lamp to one of the GP0 pins (GPO0~GPO15), and control the pin to output high and low levels;
* @n Hardware connection: connect the LED pin to the corresponding output pin
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [Arya](xue.peng@dfrobot.com)
* @version V1.0
* @date 2022-03-09
* @url https://github.com/DFRobot/DFRobot_CH423
*/
#include "DFRobot_CH423.h"
//Disable the macro, and the blink function will shift to the pin GPO0 of GPO pins, otherwise, it will be implemented by the pin GPIO0 of GPIO pins
#define GPIO_ENABLE
DFRobot_CH423 ch423;
void setup() {
Serial.begin(115200);
while(!Serial){
}
ch423.begin();
/**
* @fn pinMode
* @brief Set the mode of the pin group, this module contains 2 groups of pins: GPIO(GPIO0~GPIO7) and GPO(GPO0~GPO15).
* @note This module sets the mode by group, the pins in a group can only be set as one mode at the same time, and the pins in different groups can be set as different modes
* @param group Pin group parameter, ePinGroup_t enum variable member
* @n eGPIO Bi-directional I/O pin, GPIO0-GPIO7, which can be set as input(eINPUT) or output(eOUTPUT) mode, setting to other modes is invalid
* @n eGPO/eGPO0_7/eGPO8_15 The three parameters indicate the same meaning, set the mode of GPO group pins, select the parameter and GPO pins can only be configured as open-drain(eOPEN_DRAIN) or push-pull(ePUSH_PULL) output mode, other modes are invalid
* @param mode Group pin mode parameter, as member enum variable of eMode_t
* @n eINPUT GPIO pin input mode, at high level when floating, this mode can only be used for eGPIO digital ports
* @n eOUTPUT GPIO pin output mode, can output high and low levels, this mode can only be used for eGPIO digital ports
* @n eOPEN_DRAIN GPO pin open-drain output mode, the GPO pins only output low level or don't output in this mode, it can only be used for eGPO group digital ports
* @n ePUSH_PULL GPO pin push-pull output mode, the GPO pin can output high or low level in this mode, it can only be used for eGPO digital ports
*/
#ifdef GPIO_ENABLE
ch423.pinMode(ch423.eGPIO, ch423.eOUTPUT);
#else
ch423.pinMode(ch423.eGPO, ch423.ePUSH_PULL);
#endif
}
void loop() {
#ifdef GPIO_ENABLE
/**
* @fn
* @brief Set the pin outputting high or low level
* @param gpioPin GPIO group pins, eGPIOPin_t enum variable member
* @n eGPIO0 Bi-directional I/O pin, GPIO0, indicates setting the output value of pin GPIO0
* @n eGPIO1 Bi-directional I/O pin, GPIO1, indicates setting the output value of pin GPIO1
* @n eGPIO2 Bi-directional I/O pin, GPIO2, indicates setting the output value of pin GPIO2
* @n eGPIO3 Bi-directional I/O pin, GPIO3, indicates setting the output value of pin GPIO3
* @n eGPIO4 Bi-directional I/O pin, GPIO4, indicates setting the output value of pin GPIO4
* @n eGPIO5 Bi-directional I/O pin, GPIO5, indicates setting the output value of pin GPIO5
* @n eGPIO6 Bi-directional I/O pin, GPIO6, indicates setting the output value of pin GPIO6
* @n eGPIO7 Bi-directional I/O pin, GPIO7, indicates setting the output value of pin GPIO7
* @n eGPIOTotal sets the values of all GPIO group pins, when using this parameter, bit0~bit7 of parameter level are valid values, corresponding to the output values of pins GPIO0~GPIO7 respectively.
* @param level Output level
* @n HIGH or 1 Parameter level, bit0 in 8-bit data is valid, which indicates outputting high level
* @n LOW or 0 Parameter level, bit0 in 8-bit data is valid, which indicates outputting low level
* @n 0x00~0xFF If parameter gpioPin is GPIOTotal, bit0~bit7 of parameter level are valid data, corresponding to GPIO0~GPIO7 pins respectively.
*/
ch423.digitalWrite(/* gpio= */ch423.eGPIO0, /* level= */HIGH); // turn the LED on (HIGH is the voltage level)
//ch423.digitalWrite(/* gpio= */ch423.eGPIOTotal, /* level= */0xFF); // set all pins in GPIO group(GPIO0~GPIO7) outputting high level
delay(1000); // wait for a second
ch423.digitalWrite(/* gpio= */ch423.eGPIO0, /* level= */LOW); // turn the LED off by making the voltage LOW
//ch423.digitalWrite(/* gpio= */ch423.eGPIOTotal, /* level= */0x00); // set all pins in GPIO group(GPIO0~GPIO7) outputting low level
delay(1000); // wait for a second
#else
/**
* @fn digitalWrite
* @brief Set the pin outputting high and low level, or control to output or stop (interrupt) low level
* @param gpoPin eGPOPin_t enum variable member
* @n eGPO0 GPO pin, GPO0, indicates setting the output value of pin GPO0
* @n eGPO1 GPO pin, GPO1, indicates setting the output value of pin GPO1
* @n eGPO2 GPO pin, GPO2, indicates setting the output value of pin GPO2
* @n eGPO3 GPO pin, GPO3, indicates setting the output value of pin GPO3
* @n eGPO4 GPO pin, GPO4, indicates setting the output value of pin GPO4
* @n eGPO5 GPO pin, GPO5, indicates setting the output value of pin GPO5
* @n eGPO6 GPO pin, GPO6, indicates setting the output value of pin GPO6
* @n eGPO7 GPO pin, GPO7, indicates setting the output value of pin GPO7
* @n eGPO8 GPO pin, GPO8, indicates setting the output value of pin GPO8
* @n eGPO9 GPO pin, GPO9, indicates setting the output value of pin GPO9
* @n eGPO10 GPO pin, GPO10, indicates setting the output value of pin GPO10
* @n eGPO11 GPO pin, GPO11, indicates setting the output value of pin GPO11
* @n eGPO12 GPO pin, GPO12, indicates setting the output value of pin GPO12
* @n eGPO13 GPO pin, GPO13, indicates setting the output value of pin GPO13
* @n eGPO14 GPO pin, GPO14, indicates setting the output value of pin GPO14
* @n eGPO15 GPO pin, GPO15, indicates setting the output value of pin GPO15
* @n eGPOTotal sets all the GPO pins of 0~15, when using this parameter, the bit0~bit7 of the parameter 8-bit data level correspond to the output value of GPO0~GPO7 or GPO8~GPO15 pins respectively
* @param level Output level or output (stop) low level signal
* @n HIGH or 1 When GPO pin group is set to push-pull output mode, output high; for open-drain mode, output low level
* @n LOW or 0 When GPO pin group is set to push-pull output mode, output low, for open-drain mode, no signal output
* @n 0x00~0xFF When gpoPin parameter is eGPOTotal, bit0~bit7 of level are all valid data, corresponding to pin GPO0~GPO7 or GPO8~GPO15
*/
ch423.digitalWrite(/* gpo= */ch423.eGPO0, /* level= */HIGH); // turn the LED on (HIGH is the voltage level)
//ch423.digitalWrite(/* gpo= */ch423.eGPOTotal, /* level= */0xFF); // set all pins in GPO group(GPO0~GPO15) outputting high level
delay(1000); // wait for a second
ch423.digitalWrite(/* gpio= */ch423.eGPO0, /* level= */LOW); // turn the LED off by making the voltage LOW
//ch423.digitalWrite(/* gpo= */ch423.eGPOTotal, /* level= */0x00); // set all pins in GPO group(GPO0~GPO15) outputting low level
delay(1000); // wait for a second
#endif
}
Sample Code 2 - group.ino
/*!
* @file group.ino
* @brief This demo is mainly used to demonstrate how to operate the digital ports of a group at a time. This module has 2 groups of digital pins, GPIO group pins and GPO pins, and GPO pins are divided into GPO0_7 group pins and GPO8_15 group pins.
* @n GPIO group pins: can be used as both input pins and output pins
* @n GPO group pins: can only be used as output pins, there are 2 output modes, push-pull and open-drain output mode
* @n Users can operate the pins by group according to the API function provided
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [Arya](xue.peng@dfrobot.com)
* @version V1.0
* @date 2022-03-14
* @url https://github.com/DFRobot/DFRobot_CH423
*/
#include "DFRobot_CH423.h"
#define GROUP_GPIO_INPUT 0
#define GROUP_GPIO_OUTPUT 1
#define GROUP_GPO 2
//Demo function switch
#define DEMO_FUN_SWITCH GROUP_GPIO_INPUT
//#define DEMO_FUN_SWITCH GROUP_GPIO_OUTPUT
//#define DEMO_FUN_SWITCH GROUP_GPO
DFRobot_CH423 ch423;
void setup() {
Serial.begin(115200);
while(!Serial){
}
ch423.begin();
/**
* @fn pinMode
* @brief Set the mode of the pin groups, this module contains 2 groups of pins: GPIO(GPIO0~GPIO7) and GPO(GPO0~GPO15).
* @note This module sets the mode by group, the pins in a group can only be set as one mode at the same time, and the pins in different groups can be set as different modes
* @param group Pin group parameter, ePinGroup_t enum variable member
* @n eGPIO Bi-directional I/O pin, GPIO0~GPIO7, which can be set as input(eINPUT) or output(eOUTPUT) mode, setting to other modes is invalid
* @n eGPO/eGPO0_7/eGPO8_15 The three parameters indicate the same meaning, set the mode of GPO group pins, select the parameter and GPO pins can only be configured as open-drain(eOPEN_DRAIN) or push-pull(ePUSH_PULL) output mode, other modes are invalid
* @param mode Group pin mode parameter, enum variable member for eMode_t
* @n eINPUT GPIO pin input mode, at high level when floating, this mode can only be used for eGPIO digital ports
* @n eOUTPUT GPIO pin output mode, can output high and low levels, this mode can only be used for eGPIO digital ports
* @n eOPEN_DRAIN GPO pin open-drain output mode, the GPO pins only output low level or don't output in this mode, it can only be used for eGPO group digital ports
* @n ePUSH_PULL GPO pin push-pull output mode, the GPO pin can output high or low level in this mode, it can only be used for eGPO digital ports
*/
#if(DEMO_FUN_SWITCH == GROUP_GPIO_INPUT)
ch423.pinMode(ch423.eGPIO, ch423.eINPUT);
#elif(DEMO_FUN_SWITCH == GROUP_GPIO_OUTPUT)
ch423.pinMode(ch423.eGPIO, ch423.eOUTPUT);
//Set GPIO group pin outputting 0xF0, bit0~bit7 of 0xF0 correspond to GPIO0~GPIO7 of GPIO group respectively
DFRobot_CH423::uGroupValue_t gpio;
gpio.GPO0 = 0; //Set pin GPIO0 outputting 0
gpio.GPO1 = 0; //Set pin GPIO1 outputting 0
gpio.GPO2 = 0; //Set pin GPIO2 outputting 0
gpio.GPO3 = 0; //Set pin GPIO3 outputting 0
gpio.GPO4 = 1; //Set pin GPIO4 outputting 1
gpio.GPO5 = 1; //Set pin GPIO5 outputting 1
gpio.GPO6 = 1; //Set pin GPIO6 outputting 1
gpio.GPO7 = 1; //Set pin GPIO7 outputting 1
//gpio.GPIO = 0xF0
ch423.digitalWrite(ch423.eGPIO, gpio); //Set GPIO0~GPIO3 outputting 0, GPIO4~GPIO7 outputting 1
//ch423.digitalWrite(ch423.eGPIO, 0xF0); //Set GPIO0~GPIO3 outputting 0, GPIO4~GPIO7 outputting 1
//ch423.digitalWrite(ch423.eGPIOTotal, 0xF0); //Set GPIO0~GPIO3 outputting 0, GPIO4~GPIO7 outputting 1
Serial.print("GPIO0 OUPUT: ");Serial.println(gpio.GPIO0);
Serial.print("GPIO1 OUPUT: ");Serial.println(gpio.GPIO1);
Serial.print("GPIO2 OUPUT: ");Serial.println(gpio.GPIO2);
Serial.print("GPIO3 OUPUT: ");Serial.println(gpio.GPIO3);
Serial.print("GPIO4 OUPUT: ");Serial.println(gpio.GPIO4);
Serial.print("GPIO5 OUPUT: ");Serial.println(gpio.GPIO5);
Serial.print("GPIO6 OUPUT: ");Serial.println(gpio.GPIO6);
Serial.print("GPIO7 OUPUT: ");Serial.println(gpio.GPIO7);
#else
ch423.pinMode(ch423.eGPO, ch423.ePUSH_PULL);
//Set GPO group pins outputting 0x55F0, bit0~bit15 of 0x55F0 correspond to GPO0~GPIO15 of GPO group respectively
DFRobot_CH423::uGroupValue_t gpo;
gpo.GPO0 = 0; //Set pin GPO0 outputting 0
gpo.GPO1 = 0; //Set pin GPO1 outputting 0
gpo.GPO2 = 0; //Set pin GPO2 outputting 0
gpo.GPO3 = 0; //Set pin GPO3 outputting 0
gpo.GPO4 = 1; //Set pin GPO4 outputting 1
gpo.GPO5 = 1; //Set pin GPO5 outputting 1
gpo.GPO6 = 1; //Set pin GPO6 outputting 1
gpo.GPO7 = 1; //Set pin GPO7 outputting 1
gpo.GPO8 = 1; //Set pin GPO0 outputting 1
gpo.GPO9 = 0; //Set pin GPO1 outputting 0
gpo.GPO10 = 1; //Set pin GPO2 outputting 1
gpo.GPO11 = 0; //Set pin GPO3 outputting 0
gpo.GPO12 = 1; //Set pin GPO4 outputting 1
gpo.GPO13 = 0; //Set pin GPO5 outputting 0
gpo.GPO14 = 1; //Set pin GPO6 outputting 1
gpo.GPO15 = 0; //Set pin GPO7 outputting 0
//gpo.GPO0_7 = 0xF0;
//gpo.GPO8_15 = 0x55;
//gpo.GPO = 0x55F0;
ch423.digitalWrite(ch423.eGPO0_7, 0xF0); //only set GPO0~GPO7 outputting 0xF0
ch423.digitalWrite(ch423.eGPO8_15, (0x55 << 8)); //only set GPO8~GPO15 outputting 0x55
//ch423.digitalWrite(ch423.eGPO, gpo); //set pin GPO15~GPO0 outputting 0x55F0
//ch423.digitalWrite(ch423.eGPO, 0x55F0); //set pin GPO15~GPO0 outputting 0x55F0
Serial.print("GPO0 OUPUT: ");Serial.println(gpo.GPO0);
Serial.print("GPO1 OUPUT: ");Serial.println(gpo.GPO1);
Serial.print("GPO2 OUPUT: ");Serial.println(gpo.GPO2);
Serial.print("GPO3 OUPUT: ");Serial.println(gpo.GPO3);
Serial.print("GPO4 OUPUT: ");Serial.println(gpo.GPO4);
Serial.print("GPO5 OUPUT: ");Serial.println(gpo.GPO5);
Serial.print("GPO6 OUPUT: ");Serial.println(gpo.GPO6);
Serial.print("GPO7 OUPUT: ");Serial.println(gpo.GPO7);
Serial.print("GPO8 OUPUT: ");Serial.println(gpo.GPO8);
Serial.print("GPO9 OUPUT: ");Serial.println(gpo.GPO9);
Serial.print("GPO10 OUPUT: ");Serial.println(gpo.GPO10);
Serial.print("GPO11 OUPUT: ");Serial.println(gpo.GPO11);
Serial.print("GPO12 OUPUT: ");Serial.println(gpo.GPO12);
Serial.print("GPO13 OUPUT: ");Serial.println(gpo.GPO13);
Serial.print("GPO14 OUPUT: ");Serial.println(gpo.GPO14);
Serial.print("GPO15 OUPUT: ");Serial.println(gpo.GPO15);
#endif
}
void loop() {
#if(DEMO_FUN_SWITCH == GROUP_GPIO_INPUT)
//Query the value of each GPIO input pin (GPIO0~GPIO7) once in 1s
//0~7 bit of value correspond to the pin status of pins GPIO0~GPIO7 respectively
uint8_t value = ch423.digitalRead(ch423.eGPIOTotal);
Serial.print("The group of GPIO's pins state are 0x");
Serial.println(value, HEX);
delay(1000);
#endif
}
Sample Code 3 - input.ino
/*!
* @file input.ino
* @brief After pressing the button, the serial print button is pressed
* @note Only the pins in GPIO group can be set to input mode for this module.
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [Arya](xue.peng@dfrobot.com)
* @version V1.0
* @date 2022-03-09
* @url https://github.com/DFRobot/DFRobot_CH423
*/
#include "DFRobot_CH423.h"
DFRobot_CH423 ch423;
void setup() {
Serial.begin(115200);
while(!Serial){
}
ch423.begin();
/**
* @fn pinMode
* @brief Set the mode of the pin groups, this module contains 2 groups of pins: GPIO(GPIO0~GPIO7) and GPO(GPO0~GPO15).
* @note This module sets the mode by group, the pins in a group can only be set as one mode at the same time, and the pins in different groups can be set as different modes.
* @param group Pin group parameter, ePinGroup_t enum variable member
* @n eGPIO Bi-directional I/O pin, GPIO0-GPIO7, which can be set as input(eINPUT) or output(eOUTPUT) mode, setting to other modes is invalid
* @n eGPO/eGPO0_7/eGPO8_15 The three parameters indicate the same meaning, set the mode of GPO group pins, select the parameter and GPO pins can only be configured as open-drain(eOPEN_DRAIN) or push-pull(ePUSH_PULL) output mode, other modes are invalid
* @param mode Group pin mode parameter, as member enum variable of eMode_t
* @n eINPUT GPIO pin input mode, at high level when floating, this mode can only be used for eGPIO digital ports
* @n eOUTPUT GPIO pin output mode, can output high and low levels, this mode can only be used for eGPIO digital ports
* @n eOPEN_DRAIN GPO pin open-drain output mode, the GPO pins only output low level or don't output in this mode, it can only be used for eGPO group digital ports
* @n ePUSH_PULL GPO pin push-pull output mode, the GPO pin can output high or low level in this mode, it can only be used for eGPO digital ports
*/
ch423.pinMode(ch423.eGPIO, ch423.eINPUT);
}
void loop() {
/**
* @fn digitalRead
* @brief Read the level status values of GPIO group pins
* @param pin GPIO group pins, eGPIOPin_t enum variable member
* @n eGPIO0 Bi-directional I/O pin, GPIO0, indicates reading level status of pin GPIO0, 0 for low level, 1 for high level
* @n eGPIO1 Bi-directional I/O pin, GPIO1, indicates reading level status of pin GPIO1, 0 for low level, 1 for high level
* @n eGPIO2 Bi-directional I/O pin, GPIO2, indicates reading level status of pin GPIO2, 0 for low level, 1 for high level
* @n eGPIO3 Bi-directional I/O pin, GPIO3, indicates reading level status of pin GPIO3, 0 for low level, 1 for high level
* @n eGPIO4 Bi-directional I/O pin, GPIO4, indicates reading level status of pin GPIO4, 0 for low level, 1 for high level
* @n eGPIO5 Bi-directional I/O pin, GPIO5, indicates reading level status of pin GPIO5, 0 for low level, 1 for high level
* @n eGPIO6 Bi-directional I/O pin, GPIO6, indicates reading level status of pin GPIO6, 0 for low level, 1 for high level
* @n eGPIO7 Bi-directional I/O pin, GPIO7, indicates reading level status of pin GPIO7, 0 for low level, 1 for high level
* @n eGPIOTotal Bi-directional I/O pin, GPIO group pins GPIO0~GPIO7 indicate reading level status of all GPIO group pins, the returned value bit0~bit8 represents the level of GPIO0~GPIO7 pins respectively
* @return Level status value
*/
uint8_t value = ch423.digitalRead(/*pin = */ch423.eGPIO0);
if(value){
Serial.println("Button press!");
delay(200);
}else{
//Serial.println("Button release!");
}
}
Sample Code 4 - ioInterrupt.ino
/*!
* @file ioInterrupt.ino
* @brief This demo is used to demonstrate the detection of rising edge, falling edge or double edge interrupt on the GPIO pin
* @note When the module is configured as rising edge, falling edge or double edge interrupt, the MCU external interrupt should use falling edge interrupt to reduce errors
* @n After burning this demo, pin GPO15 of the module needs to be connected to the corresponding external interrupt pin of MCU
* @n Hardware connection
* @n ---------------------------------------------------------
* @n module |UNO |mega2560 |M0 |ESP32 |ESP8266 |microbit |
* @n VCC |VCC |VCC |VCC |VCC |VCC |VCC |
* @n GND |GND |GND |GND |GND |GND |GND |
* @n SCL |SCL |SCL |SCL |SCL |SCL |SCL |
* @n SDA |SDA |SDA |SDA |SDA |SDA |SDA |
* @n GPO15 |2/D2 |2/D2 |2 |2 |12 |P9 |
* @n ---------------------------------------------------------
* @note If the level of any pin of GPIO0~GPIO7 changes, GPO15 will output a low level signal, and it outputs high level until the levels of GPIO0~GPIO7 pins return to the initial level.
* @n Leonardo is not supported
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [Arya](xue.peng@dfrobot.com)
* @version V1.0
* @date 2022-03-11
* @url https://github.com/DFRobot/DFRobot_CH423
*/
#include "DFRobot_CH423.h"
DFRobot_CH423 ch423;
#if defined(ARDUINO_BBC_MICROBIT)
#define INT_PIN 9 //Connect pin GPO15 of the module to pin 9 of the MCU. For some MCUs, download the codes first and then connect hardware.
#elif defined(ESP8266)
#define INT_PIN 12 //Connect pin GPO15 of the module to pin 12 of the MCU. For some MCUs, download the codes first and then connect hardware.
#else
#define INT_PIN 2 //Connect pin GPO15 of the module to pin 2 of the MCU. For some MCUs, download the codes first and then connect hardware.
#endif
bool intFlag = false; //INT interrupt sign
/*Interrupt service function, prototype void func(int index), index: the number of the pin that is interrupted*/
void func(int index){
String description = ch423.pinDescription(/*pin = */(DFRobot_CH423::eGPIOPin_t)index);
Serial.print(description);
Serial.println(" Interruption occurs!");
}
void setup() {
Serial.begin(115200);
while(!Serial){
}
ch423.begin();
/**
* @fn pinMode
* @brief Set the mode of the pin groups, this module contains 2 groups of pins: GPIO(GPIO0~GPIO7) and GPO(GPO0~GPO15).
* @note This module sets the mode by group, the pins in a group can only be set as one mode at the same time, and the pins in different groups can be set as different modes
* @param group Pin group parameter, ePinGroup_t enum variable member
* @n eGPIO Bi-directional I/O pin, GPIO0~GPIO7, which can be set as input(eINPUT) or output(eOUTPUT) mode, setting to other modes is invalid
* @n eGPO/eGPO0_7/eGPO8_15 The three parameters indicate the same meaning, set the mode of GPO group pins, select the parameter and GPO pins can only be configured as open-drain(eOPEN_DRAIN) or push-pull(ePUSH_PULL) output mode, other modes are invalid
* @param mode Group pin mode parameter, as member enum variable of eMode_t
* @n eINPUT GPIO pin input mode, at high level when floating, this mode can only be used for eGPIO digital ports
* @n eOUTPUT GPIO pin output mode, can output high and low levels, this mode can only be used for eGPIO digital ports
* @n eOPEN_DRAIN GPO pin open-drain output mode, the GPO pins only output low level or don't output in this mode, it can only be used for eGPO group digital ports
* @n ePUSH_PULL GPO pin push-pull output mode, the GPO pin can output high or low level in this mode, it can only be used for eGPO digital ports
*/
ch423.pinMode(ch423.eGPIO, ch423.eINPUT);
/**
* @fn attachInterrupt
* @brief Set the external interrupt mode and interrupt service function of GPIO pins
* @note Pin GPO15 of CH423 module is used to indicate whether an interrupt occurs on GPIO0~GPIO7 in interrupt mode, if an interrupt occurs on a pin, GPO15 will output a low level continuously, otherwise it will output a high level.
* @n When an interrupt occurs on a pin, trigger continuously, if there is interrupt occurring on other pins, level of pin GPO15 keeps low without changing
* @param gpioPin GPIO group pins, eGPIOPin_t enum variable member
* @n eGPIO0 Bi-directional I/O pin, GPIO0, indicates setting the external interrupt mode and interrupt service function of pin GPIO0
* @n eGPIO1 Bi-directional I/O pin, GPIO1, indicates setting the external interrupt mode and interrupt service function of pin GPIO1
* @n eGPIO2 Bi-directional I/O pin, GPIO2, indicates setting the external interrupt mode and interrupt service function of pin GPIO2
* @n eGPIO3 Bi-directional I/O pin, GPIO3, indicates setting the external interrupt mode and interrupt service function of pin GPIO3
* @n eGPIO4 Bi-directional I/O pin, GPIO4, indicates setting the external interrupt mode and interrupt service function of pin GPIO4
* @n eGPIO5 Bi-directional I/O pin, GPIO5, indicates setting the external interrupt mode and interrupt service function of pin GPIO5
* @n eGPIO6 Bi-directional I/O pin, GPIO6, indicates setting the external interrupt mode and interrupt service function of pin GPIO6
* @n eGPIO7 Bi-directional I/O pin, GPIO7, indicates setting the external interrupt mode and interrupt service function of pin GPIO7
* @n eGPIOTotal Set the values of all GPIO group pins, which indicates setting GPIO0~GPIO7 to the same interrupt mode and interrupt service function
* @param mode Interrupt mode
* @n eLOW Low level interrupt, initial level of the GPIO pin is high, if the GPIO pin detect a low level, pin GPO15 will output a low level signal
* @n eHIGH High level interrupt, initial level of the GPIO pin is low, if the GPIO pin detect a high level, pin GPO15 will output a low level signal
* @n eRISING Rising edge interrupt, initial level of the GPIO pin is low, if the GPIO pin detect a rising edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @n eFALLING Falling edge interrupt, initial level of the GPIO pin is high, if the GPIO pin detect a falling edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @n eCHANGE Double edge interrupt, initial level of the GPIO pin is high, and the initial level switches once for each interrupt that occurs. If the GPIO pin detect falling edge or rising edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @note If the level of any pin of GPIO0-GPIO7 changes, GPO15 will output a low level signal, and it outputs high level until the levels of GPIO0~GPIO7 pins return to the initial level
* @param cb Point to interrupt service function
*/
ch423.attachInterrupt(/* gpioPin= */ch423.eGPIO0, /* mode= */ch423.eFALLING, /* cb= */func);
ch423.enableInterrupt();
/**
* @brief Enable the external interrupt pin of MCU.
* @param pin: The external pin of MCU.
* @n UNO: The external pin is 2, 3,
* @n Leonardo: (Not support)The external pin is 0(RX), 1(TX), 2(SCL), 3(SDA), but 0, 1, 2, 3 can't use in this demo
* @n Mega2560: The external pin is 2, 3, 21, 20, 19, 18.
* @n microbit: The external pin is 0~20(P0-P20)
* @n ESP32, ESP8266, M0: The external pin is all digital Pin and analog pin.
* @param fun: Pointer to guide interrupt service function.
* @param mode: Interrupt trigger mode.
* @n UNO/Leonardo/Mega2560: Interrupt Modes: RISING(0x03), FALLING(0x02), CHANGE(0x01), LOW(0x00)
* @n 8266/esp32: Interrupt Modes: RISING(0x01), FALLING(0x02), CHANGE(0x03), ONLOW(0x04), ONHIGH(0x05),
* @n M0: Interrupt Modes: RISING(0x04), FALLING(0x03), CHANGE(0x02), HIGH(0x01), LOW(0x00)
* @n microbit: Interrupt Modes: RISING(0x04), FALLING(0x03), CHANGE(0x02)
*/
pinMode(INT_PIN, INPUT_PULLUP);
attachInterrupt(/*pin=*/digitalPinToInterrupt(INT_PIN),/*fun=*/notify,/*mode =*/FALLING);
Serial.println("Set done!");
}
/*Interrupt service function*/
void notify(){
intFlag = true;
}
void loop() {
if(intFlag){
intFlag = false;
ch423.pollInterrupts();
}
yield();
}
Sample Code 5 - pollInterrupt.ino
/*!
* @file ioInterrupt.ino
* @brief This demo is used to demonstrate the detection of rising edge, falling edge or double edge interrupt on the GPIO pin
* @note When the module is configured as rising edge, falling edge or double edge interrupt, the MCU external interrupt should use falling edge interrupt to reduce errors
* @n After burning this demo, pin GPO15 of the module needs to be connected to the corresponding external interrupt pin of MCU
* @n Hardware connection
* @n ---------------------------------------------------------
* @n module |UNO |mega2560 |M0 |ESP32 |ESP8266 |microbit |
* @n VCC |VCC |VCC |VCC |VCC |VCC |VCC |
* @n GND |GND |GND |GND |GND |GND |GND |
* @n SCL |SCL |SCL |SCL |SCL |SCL |SCL |
* @n SDA |SDA |SDA |SDA |SDA |SDA |SDA |
* @n GPO15 |2/D2 |2/D2 |2 |2 |12 |P9 |
* @n ---------------------------------------------------------
* @note If the level of any pin of GPIO0~GPIO7 changes, GPO15 will output a low level signal, and it outputs high level until the levels of GPIO0~GPIO7 pins return to the initial level.
* @n Leonardo is not supported
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [Arya](xue.peng@dfrobot.com)
* @version V1.0
* @date 2022-03-11
* @url https://github.com/DFRobot/DFRobot_CH423
*/
#include "DFRobot_CH423.h"
DFRobot_CH423 ch423;
#if defined(ARDUINO_BBC_MICROBIT)
#define INT_PIN 9 //Connect pin GPO15 of the module to pin 9 of the MCU. For some MCUs, download the codes first and then connect hardware.
#elif defined(ESP8266)
#define INT_PIN 12 //Connect pin GPO15 of the module to pin 12 of the MCU. For some MCUs, download the codes first and then connect hardware.
#else
#define INT_PIN 2 //Connect pin GPO15 of the module to pin 2 of the MCU. For some MCUs, download the codes first and then connect hardware.
#endif
bool intFlag = false; //INT interrupt sign
/*Interrupt service function, prototype void func(int index), index: the number of the pin that is interrupted*/
void func(int index){
String description = ch423.pinDescription(/*pin = */(DFRobot_CH423::eGPIOPin_t)index);
Serial.print(description);
Serial.println(" Interruption occurs!");
}
void setup() {
Serial.begin(115200);
while(!Serial){
}
ch423.begin();
/**
* @fn pinMode
* @brief Set the mode of the pin groups, this module contains 2 groups of pins: GPIO(GPIO0~GPIO7) and GPO(GPO0~GPO15).
* @note This module sets the mode by group, the pins in a group can only be set as one mode at the same time, and the pins in different groups can be set as different modes
* @param group Pin group parameter, ePinGroup_t enum variable member
* @n eGPIO Bi-directional I/O pin, GPIO0~GPIO7, which can be set as input(eINPUT) or output(eOUTPUT) mode, setting to other modes is invalid
* @n eGPO/eGPO0_7/eGPO8_15 The three parameters indicate the same meaning, set the mode of GPO group pins, select the parameter and GPO pins can only be configured as open-drain(eOPEN_DRAIN) or push-pull(ePUSH_PULL) output mode, other modes are invalid
* @param mode Group pin mode parameter, as member enum variable of eMode_t
* @n eINPUT GPIO pin input mode, at high level when floating, this mode can only be used for eGPIO digital ports
* @n eOUTPUT GPIO pin output mode, can output high and low levels, this mode can only be used for eGPIO digital ports
* @n eOPEN_DRAIN GPO pin open-drain output mode, the GPO pins only output low level or don't output in this mode, it can only be used for eGPO group digital ports
* @n ePUSH_PULL GPO pin push-pull output mode, the GPO pin can output high or low level in this mode, it can only be used for eGPO digital ports
*/
ch423.pinMode(ch423.eGPIO, ch423.eINPUT);
/**
* @fn attachInterrupt
* @brief Set the external interrupt mode and interrupt service function of GPIO pins
* @note Pin GPO15 of CH423 module is used to indicate whether an interrupt occurs on GPIO0~GPIO7 in interrupt mode, if an interrupt occurs on a pin, GPO15 will output a low level continuously, otherwise it will output a high level.
* @n When an interrupt occurs on a pin, trigger continuously, if there is interrupt occurring on other pins, level of pin GPO15 keeps low without changing
* @param gpioPin GPIO group pins, eGPIOPin_t enum variable member
* @n eGPIO0 Bi-directional I/O pin, GPIO0, indicates setting the external interrupt mode and interrupt service function of pin GPIO0
* @n eGPIO1 Bi-directional I/O pin, GPIO1, indicates setting the external interrupt mode and interrupt service function of pin GPIO1
* @n eGPIO2 Bi-directional I/O pin, GPIO2, indicates setting the external interrupt mode and interrupt service function of pin GPIO2
* @n eGPIO3 Bi-directional I/O pin, GPIO3, indicates setting the external interrupt mode and interrupt service function of pin GPIO3
* @n eGPIO4 Bi-directional I/O pin, GPIO4, indicates setting the external interrupt mode and interrupt service function of pin GPIO4
* @n eGPIO5 Bi-directional I/O pin, GPIO5, indicates setting the external interrupt mode and interrupt service function of pin GPIO5
* @n eGPIO6 Bi-directional I/O pin, GPIO6, indicates setting the external interrupt mode and interrupt service function of pin GPIO6
* @n eGPIO7 Bi-directional I/O pin, GPIO7, indicates setting the external interrupt mode and interrupt service function of pin GPIO7
* @n eGPIOTotal Set the values of all GPIO group pins, which indicates setting GPIO0~GPIO7 to the same interrupt mode and interrupt service function
* @param mode Interrupt mode
* @n eLOW Low level interrupt, initial level of the GPIO pin is high, if the GPIO pin detect a low level, pin GPO15 will output a low level signal
* @n eHIGH High level interrupt, initial level of the GPIO pin is low, if the GPIO pin detect a high level, pin GPO15 will output a low level signal
* @n eRISING Rising edge interrupt, initial level of the GPIO pin is low, if the GPIO pin detect a rising edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @n eFALLING Falling edge interrupt, initial level of the GPIO pin is high, if the GPIO pin detect a falling edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @n eCHANGE Double edge interrupt, initial level of the GPIO pin is high, and the initial level switches once for each interrupt that occurs. If the GPIO pin detect falling edge or rising edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @note If the level of any pin of GPIO0-GPIO7 changes, GPO15 will output a low level signal, and it outputs high level until the levels of GPIO0~GPIO7 pins return to the initial level
* @param cb Point to interrupt service function
*/
ch423.attachInterrupt(/* gpioPin= */ch423.eGPIO0, /* mode= */ch423.eFALLING, /* cb= */func);
ch423.enableInterrupt();
/**
* @brief Enable the external interrupt pin of MCU.
* @param pin: The external pin of MCU.
* @n UNO: The external pin is 2, 3,
* @n Leonardo: (Not support)The external pin is 0(RX), 1(TX), 2(SCL), 3(SDA), but 0, 1, 2, 3 can't use in this demo
* @n Mega2560: The external pin is 2, 3, 21, 20, 19, 18.
* @n microbit: The external pin is 0~20(P0-P20)
* @n ESP32, ESP8266, M0: The external pin is all digital Pin and analog pin.
* @param fun: Pointer to guide interrupt service function.
* @param mode: Interrupt trigger mode.
* @n UNO/Leonardo/Mega2560: Interrupt Modes: RISING(0x03), FALLING(0x02), CHANGE(0x01), LOW(0x00)
* @n 8266/esp32: Interrupt Modes: RISING(0x01), FALLING(0x02), CHANGE(0x03), ONLOW(0x04), ONHIGH(0x05),
* @n M0: Interrupt Modes: RISING(0x04), FALLING(0x03), CHANGE(0x02), HIGH(0x01), LOW(0x00)
* @n microbit: Interrupt Modes: RISING(0x04), FALLING(0x03), CHANGE(0x02)
*/
pinMode(INT_PIN, INPUT_PULLUP);
attachInterrupt(/*pin=*/digitalPinToInterrupt(INT_PIN),/*fun=*/notify,/*mode =*/FALLING);
Serial.println("Set done!");
}
/*Interrupt service function*/
void notify(){
intFlag = true;
}
void loop() {
if(intFlag){
intFlag = false;
ch423.pollInterrupts();
}
yield();
}
Sample Code 6 - sleep.ino
/*!
* @file ioInterrupt.ino
* @brief This demo is used to demonstrate the detection of rising edge, falling edge or double edge interrupt on the GPIO pin
* @note When the module is configured as rising edge, falling edge or double edge interrupt, the MCU external interrupt should use falling edge interrupt to reduce errors
* @n After burning this demo, pin GPO15 of the module needs to be connected to the corresponding external interrupt pin of MCU
* @n Hardware connection
* @n ---------------------------------------------------------
* @n module |UNO |mega2560 |M0 |ESP32 |ESP8266 |microbit |
* @n VCC |VCC |VCC |VCC |VCC |VCC |VCC |
* @n GND |GND |GND |GND |GND |GND |GND |
* @n SCL |SCL |SCL |SCL |SCL |SCL |SCL |
* @n SDA |SDA |SDA |SDA |SDA |SDA |SDA |
* @n GPO15 |2/D2 |2/D2 |2 |2 |12 |P9 |
* @n ---------------------------------------------------------
* @note If the level of any pin of GPIO0~GPIO7 changes, GPO15 will output a low level signal, and it outputs high level until the levels of GPIO0~GPIO7 pins return to the initial level.
* @n Leonardo is not supported
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [Arya](xue.peng@dfrobot.com)
* @version V1.0
* @date 2022-03-11
* @url https://github.com/DFRobot/DFRobot_CH423
*/
#include "DFRobot_CH423.h"
DFRobot_CH423 ch423;
#if defined(ARDUINO_BBC_MICROBIT)
#define INT_PIN 9 //Connect pin GPO15 of the module to pin 9 of the MCU. For some MCUs, download the codes first and then connect hardware.
#elif defined(ESP8266)
#define INT_PIN 12 //Connect pin GPO15 of the module to pin 12 of the MCU. For some MCUs, download the codes first and then connect hardware.
#else
#define INT_PIN 2 //Connect pin GPO15 of the module to pin 2 of the MCU. For some MCUs, download the codes first and then connect hardware.
#endif
bool intFlag = false; //INT interrupt sign
/*Interrupt service function, prototype void func(int index), index: the number of the pin that is interrupted*/
void func(int index){
String description = ch423.pinDescription(/*pin = */(DFRobot_CH423::eGPIOPin_t)index);
Serial.print(description);
Serial.println(" Interruption occurs!");
}
void setup() {
Serial.begin(115200);
while(!Serial){
}
ch423.begin();
/**
* @fn pinMode
* @brief Set the mode of the pin groups, this module contains 2 groups of pins: GPIO(GPIO0~GPIO7) and GPO(GPO0~GPO15).
* @note This module sets the mode by group, the pins in a group can only be set as one mode at the same time, and the pins in different groups can be set as different modes
* @param group Pin group parameter, ePinGroup_t enum variable member
* @n eGPIO Bi-directional I/O pin, GPIO0~GPIO7, which can be set as input(eINPUT) or output(eOUTPUT) mode, setting to other modes is invalid
* @n eGPO/eGPO0_7/eGPO8_15 The three parameters indicate the same meaning, set the mode of GPO group pins, select the parameter and GPO pins can only be configured as open-drain(eOPEN_DRAIN) or push-pull(ePUSH_PULL) output mode, other modes are invalid
* @param mode Group pin mode parameter, as member enum variable of eMode_t
* @n eINPUT GPIO pin input mode, at high level when floating, this mode can only be used for eGPIO digital ports
* @n eOUTPUT GPIO pin output mode, can output high and low levels, this mode can only be used for eGPIO digital ports
* @n eOPEN_DRAIN GPO pin open-drain output mode, the GPO pins only output low level or don't output in this mode, it can only be used for eGPO group digital ports
* @n ePUSH_PULL GPO pin push-pull output mode, the GPO pin can output high or low level in this mode, it can only be used for eGPO digital ports
*/
ch423.pinMode(ch423.eGPIO, ch423.eINPUT);
/**
* @fn attachInterrupt
* @brief Set the external interrupt mode and interrupt service function of GPIO pins
* @note Pin GPO15 of CH423 module is used to indicate whether an interrupt occurs on GPIO0~GPIO7 in interrupt mode, if an interrupt occurs on a pin, GPO15 will output a low level continuously, otherwise it will output a high level.
* @n When an interrupt occurs on a pin, trigger continuously, if there is interrupt occurring on other pins, level of pin GPO15 keeps low without changing
* @param gpioPin GPIO group pins, eGPIOPin_t enum variable member
* @n eGPIO0 Bi-directional I/O pin, GPIO0, indicates setting the external interrupt mode and interrupt service function of pin GPIO0
* @n eGPIO1 Bi-directional I/O pin, GPIO1, indicates setting the external interrupt mode and interrupt service function of pin GPIO1
* @n eGPIO2 Bi-directional I/O pin, GPIO2, indicates setting the external interrupt mode and interrupt service function of pin GPIO2
* @n eGPIO3 Bi-directional I/O pin, GPIO3, indicates setting the external interrupt mode and interrupt service function of pin GPIO3
* @n eGPIO4 Bi-directional I/O pin, GPIO4, indicates setting the external interrupt mode and interrupt service function of pin GPIO4
* @n eGPIO5 Bi-directional I/O pin, GPIO5, indicates setting the external interrupt mode and interrupt service function of pin GPIO5
* @n eGPIO6 Bi-directional I/O pin, GPIO6, indicates setting the external interrupt mode and interrupt service function of pin GPIO6
* @n eGPIO7 Bi-directional I/O pin, GPIO7, indicates setting the external interrupt mode and interrupt service function of pin GPIO7
* @n eGPIOTotal Set the values of all GPIO group pins, which indicates setting GPIO0~GPIO7 to the same interrupt mode and interrupt service function
* @param mode Interrupt mode
* @n eLOW Low level interrupt, initial level of the GPIO pin is high, if the GPIO pin detect a low level, pin GPO15 will output a low level signal
* @n eHIGH High level interrupt, initial level of the GPIO pin is low, if the GPIO pin detect a high level, pin GPO15 will output a low level signal
* @n eRISING Rising edge interrupt, initial level of the GPIO pin is low, if the GPIO pin detect a rising edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @n eFALLING Falling edge interrupt, initial level of the GPIO pin is high, if the GPIO pin detect a falling edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @n eCHANGE Double edge interrupt, initial level of the GPIO pin is high, and the initial level switches once for each interrupt that occurs. If the GPIO pin detect falling edge or rising edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @note If the level of any pin of GPIO0-GPIO7 changes, GPO15 will output a low level signal, and it outputs high level until the levels of GPIO0~GPIO7 pins return to the initial level
* @param cb Point to interrupt service function
*/
ch423.attachInterrupt(/* gpioPin= */ch423.eGPIO0, /* mode= */ch423.eFALLING, /* cb= */func);
ch423.enableInterrupt();
/**
* @brief Enable the external interrupt pin of MCU.
* @param pin: The external pin of MCU.
* @n UNO: The external pin is 2, 3,
* @n Leonardo: (Not support)The external pin is 0(RX), 1(TX), 2(SCL), 3(SDA), but 0, 1, 2, 3 can't use in this demo
* @n Mega2560: The external pin is 2, 3, 21, 20, 19, 18.
* @n microbit: The external pin is 0~20(P0-P20)
* @n ESP32, ESP8266, M0: The external pin is all digital Pin and analog pin.
* @param fun: Pointer to guide interrupt service function.
* @param mode: Interrupt trigger mode.
* @n UNO/Leonardo/Mega2560: Interrupt Modes: RISING(0x03), FALLING(0x02), CHANGE(0x01), LOW(0x00)
* @n 8266/esp32: Interrupt Modes: RISING(0x01), FALLING(0x02), CHANGE(0x03), ONLOW(0x04), ONHIGH(0x05),
* @n M0: Interrupt Modes: RISING(0x04), FALLING(0x03), CHANGE(0x02), HIGH(0x01), LOW(0x00)
* @n microbit: Interrupt Modes: RISING(0x04), FALLING(0x03), CHANGE(0x02)
*/
pinMode(INT_PIN, INPUT_PULLUP);
attachInterrupt(/*pin=*/digitalPinToInterrupt(INT_PIN),/*fun=*/notify,/*mode =*/FALLING);
Serial.println("Set done!");
}
/*Interrupt service function*/
void notify(){
intFlag = true;
}
void loop() {
if(intFlag){
intFlag = false;
ch423.pollInterrupts();
}
yield();
}
Sample Code 7 - waterLamp.ino
/*!
* @file waterLamp.ino
* @brief This demo is used to make RGB LED realize the effect of the water flowing effect
* @note 8 LEDs need to be connected to GPIO group pins, GPO0~7 group pins, or GPO8~15 group pins, you can switch GROUP_FLAG to achieve the effect of connecting to the IO port of different groups
*
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [Arya](xue.peng@dfrobot.com)
* @version V1.0
* @date 2022-03-11
* @url https://github.com/DFRobot/DFRobot_CH423
*/
#include "DFRobot_CH423.h"
#define GROUP_GPIO 0
#define GROUP_GPO0_7 1
#define GROUP_GPO8_15 2
//Control macro GROUP_FLAG to realize the connecting of the LED to the pin group of the module
#define GROUP_FLAG GROUP_GPIO
//#define GROUP_FLAG GROUP_GPO0_7
//#define GROUP_FLAG GROUP_GPO8_15
DFRobot_CH423 ch423;
void setup() {
Serial.begin(115200);
while(!Serial){
}
ch423.begin();
/**
* @fn pinMode
* @brief Set the mode of the pin groups, this module contains 2 groups of pins: GPIO(GPIO0~GPIO7) and GPO(GPO0~GPO15).
* @note This module sets the mode by group, the pins in a group can only be set as one mode at the same time, and the pins in different groups can be set as different modes
* @param group Pin group parameter, ePinGroup_t enum variable member
* @n eGPIO Bi-directional I/O pins, GPIO0~GPIO7, which can be set as input(eINPUT) or output(eOUTPUT) mode, invalid when set to other modes
* @n eGPO/eGPO0_7/eGPO8_15 The three parameters indicate the same meaning, set the mode of GPO group pins, select the parameter and GPO pins can only be configured as open-drain(eOPEN_DRAIN) or push-pull(ePUSH_PULL) output mode, other modes are invalid
* @param mode Group pin mode parameter, as member enum variable of eMode_t
* @n eINPUT GPIO pin input mode, at high level when floating, this mode can only be used for eGPIO digital ports
* @n eOUTPUT GPIO pin output mode, can output high and low levels, this mode can only be used for eGPIO digital ports
* @n eOPEN_DRAIN GPO pin open-drain output mode, the GPO pins only output low level or don't output in this mode, it can only be used for eGPO group digital ports
* @n ePUSH_PULL GPO pin push-pull output mode, the GPO pin can output high or low level in this mode, it can only be used for eGPO digital ports
*/
#if (GROUP_FLAG == GROUP_GPIO)
ch423.pinMode(ch423.eGPIO, ch423.eOUTPUT);
#else
ch423.pinMode(ch423.eGPO, ch423.ePUSH_PULL);
#endif
}
void loop() {
#if (GROUP_FLAG == GROUP_GPIO)
DFRobot_CH423::eGPIOPin_t gpioBuf[]= {ch423.eGPIO0, ch423.eGPIO1, ch423.eGPIO2, ch423.eGPIO3, ch423.eGPIO4, ch423.eGPIO5, ch423.eGPIO6, ch423.eGPIO7};
int on = 0, off;
ch423.digitalWrite(gpioBuf[on], HIGH);
for(on = 1, off = 0; on < sizeof(gpioBuf)/sizeof(DFRobot_CH423::eGPIOPin_t); on++, off++){
ch423.digitalWrite(gpioBuf[on], HIGH);
ch423.digitalWrite(gpioBuf[off], LOW);
delay(200);
}
ch423.digitalWrite(gpioBuf[off], LOW);
#elif (GROUP_FLAG == GROUP_GPO0_7)
DFRobot_CH423::eGPOPin_t gpoBuf[]= {ch423.eGPO0, ch423.eGPO1, ch423.eGPO2, ch423.eGPO3, ch423.eGPO4, ch423.eGPO5, ch423.eGPO6, ch423.eGPO7};
int on = 0, off;
ch423.digitalWrite(gpoBuf[on], HIGH);
for(on = 1, off = 0; on < sizeof(gpoBuf)/sizeof(DFRobot_CH423::eGPOPin_t); on++, off++){
ch423.digitalWrite(gpoBuf[on], HIGH);
ch423.digitalWrite(gpoBuf[off], LOW);
delay(200);
}
ch423.digitalWrite(gpoBuf[off], LOW);
#else
DFRobot_CH423::eGPOPin_t gpoBuf[]= {ch423.eGPO8, ch423.eGPO9, ch423.eGPO10, ch423.eGPO11, ch423.eGPO12, ch423.eGPO13, ch423.eGPO14, ch423.eGPO15};
int on = 0, off;
ch423.digitalWrite(gpoBuf[on], HIGH);
for(on = 1, off = 0; on < sizeof(gpoBuf)/sizeof(DFRobot_CH423::eGPOPin_t); on++, off++){
ch423.digitalWrite(gpoBuf[on], HIGH);
ch423.digitalWrite(gpoBuf[off], LOW);
delay(200);
}
ch423.digitalWrite(gpoBuf[off], LOW);
#endif
delay(200);
}
Main API Functions
typedef enum{
eGPO0, /**< General-purpose output pin, GPO0 */
eGPO1, /**< General-purpose output pin, GPO1 */
eGPO2, /**< General-purpose output pin, GPO2 */
eGPO3, /**< General-purpose output pin, GPO3 */
eGPO4, /**< General-purpose output pin, GPO4 */
eGPO5, /**< General-purpose output pin, GPO5 */
eGPO6, /**< General-purpose output pin, GPO6 */
eGPO7, /**< General-purpose output pin, GPO7 */
eGPO8, /**< General-purpose output pin, GPO8 */
eGPO9, /**< General-purpose output pin, GPO9 */
eGPO10, /**< General-purpose output pin, GPO10*/
eGPO11, /**< General-purpose output pin, GPO11*/
eGPO12, /**< General-purpose output pin, GPO12*/
eGPO13, /**< General-purpose output pin, GPO13*/
eGPO14, /**< General-purpose output pin, GPO14*/
eGPO15, /**< General-purpose output pin, GPO15*/
eGPOTotal/**< Total number of general-purpose output pins */
}eGPOPin_t;
typedef enum{
eGPIO0, /**< Bi-directional input/output pin, GPIO0*/
eGPIO1, /**< Bi-directional input/output pin, GPIO1*/
eGPIO2, /**< Bi-directional input/output pin, GPIO2*/
eGPIO3, /**< Bi-directional input/output pin, GPIO3*/
eGPIO4, /**< Bi-directional input/output pin, GPIO4*/
eGPIO5, /**< Bi-directional input/output pin, GPIO5*/
eGPIO6, /**< Bi-directional input/output pin, GPIO6*/
eGPIO7, /**< Bi-directional input/output pin, GPIO7*/
eGPIOTotal /**< Total number of bi-directional input/output pins */
}eGPIOPin_t;
typedef enum{
eGPIO, /**< Bi-directional input/output pin, GPIO0~GPIO7 */
eGPO, /**< General-purpose output pin, GPO0~GPO15 */
eGPO0_7, /**< General-purpose output pin, GPO0~GPO7 */
eGPO8_15 /**< General-purpose output pin, GPO8~GPO15 */
}ePinGroup_t;
typedef enum{
eINPUT, /**<GPIO pin input mode, at high level when floating*/
eOUTPUT, /**<GPIO pin output mode, can output high/low level*/
eOPEN_DRAIN, /**<GPO pin open-drain output mode, can only be used for eGPO0_7 and eGPO8_15 digital ports. And GPO can only output low level or do not output in this mode.*/
ePUSH_PULL /**<GPO pin push-pull output mode, can only be used for eGPO0_7 and eGPO8_15 digital ports. And GPO can output high or low level.*/
}eMode_t;
typedef enum{
eLOW, /**< configure pin interrupt, low level interrupt */
eHIGH, /**< configure pin interrupt, high level interrupt */
eRISING, /**< configure pin interrupt, rising edge interrupt */
eFALLING, /**< configure pin interrupt, falling edge interrupt */
eCHANGE /**< configure pin interrupt, double edge interrupt*/
}eInterruptMode_t;
/**
* @fn pinMode
* @brief Set pin group mode. The module includes two groups of pins: GPIO(GPIO0~GPIO7) and GPO(GPO0~GPO15)
* @note This module sets the mode by group, the pins in a group can only be set as one mode at the same time, and the pins in different groups can be set to different modes
* @param group Pin group parameter, ePinGroup_t enum variable member
* @n eGPIO Bi-directional input/output pin, GPIO0~GPIO7, this pin group can be set to input (eINPUT) or output(eOUTPUT) mode, invalid when set to other modes.
* @n eGPO/eGPO0_7/eGPO8_15 3 parameters has same meanings: set GPO group pin mode. They are for genernal-purpose output pin and can only be configured as open-drain(eOPEN_DRAIN) or push-pull(ePUSH_PULL)output mode, invalid when set to other modes.
* @param mode Group pin mode parameter, as member of eMode_t enum variable
* @n eINPUT GPIO pin input mode, at high level when floating, can only be used for eGPIO group digital port
* @n eOUTPUT GPIO pin output mode, can output high or low level, can only be used for eGPIO digital port
* @n eOPEN_DRAIN GPO pin open-drain output mode, GOP pin can only output low level or do not output in this mode. Only suitable for eGPO group digital port
* @n ePUSH_PULL GPO pin push=pull output mode, GPO pin can output high or low level in this mode. Only suitable for eGPO group digital port
*/
void pinMode(ePinGroup_t group, eMode_t mode);
void pinMode(int group, int mode);
/**
* @fn pinMode
* @brief Set pin group mode. The module includes two groups of pins: GPIO(GPIO0~GPIO7) and GPO(GPO0~GPO15)
* @note This module sets the mode by group, the pins in a group can only be set as one mode at the same time, and the pins in different groups can be set to different modes
* @param group Pin group parameter, ePinGroup_t enum variable member
* @n eGPIO Bi-directional input/output pin, GPIO0~GPIO7, this pin group can be set to input (eINPUT) or output(eOUTPUT) mode, invalid when set to other modes.
* @n eGPO/eGPO0_7/eGPO8_15 3 parameters has same meanings: set GPO group pin mode. They are for genernal-purpose output pin and can only be configured as open-drain(eOPEN_DRAIN) or push-pull(ePUSH_PULL)output mode, invalid when set to other modes.
* @param mode Group pin mode parameter, as member of eMode_t enum variable
* @n eINPUT GPIO pin input mode, at high level when floating, can only be used for eGPIO group digital port
* @n eOUTPUT GPIO pin output mode, can output high or low level, can only be used for eGPIO digital port
* @n eOPEN_DRAIN GPO pin open-drain output mode, GOP pin can only output low level or do not output in this mode. Only suitable for eGPO group digital port
* @n ePUSH_PULL GPO pin push=pull output mode, GPO pin can output high or low level in this mode. Only suitable for eGPO group digital port
*/
void pinMode(ePinGroup_t group, eMode_t mode);
void pinMode(int group, int mode);
/**
* @fn
* @brief Set pin output level
* @param gpioPin GPIO pins, eGPIOPin_t enum variable member
* @n eGPIO0 Bi-directional input/output pin, GPIO0, set output value of pin GPIO0
* @n eGPIO1 Bi-directional input/output pin, GPIO1, set output value of pin GPIO1
* @n eGPIO2 Bi-directional input/output pin, GPIO2, set output value of pin GPIO2
* @n eGPIO3 Bi-directional input/output pin, GPIO3, set output value of pin GPIO3
* @n eGPIO4 Bi-directional input/output pin, GPIO4, set output value of pin GPIO4
* @n eGPIO5 Bi-directional input/output pin, GPIO5, set output value of pin GPIO5
* @n eGPIO6 Bi-directional input/output pin, GPIO6, set output value of pin GPIO6
* @n eGPIO7 Bi-directional input/output pin, GPIO7, set output value of pin GPIO7
* @n eGPIOTotal Set values of all pins in GPIO group. When using this parameter, bit0~bit7 of parameter level are valid value, corresponding to output of GPIO0~GPIO7
* @param level Output level
* @n HIGH or 1 Parameter level, bit0 in 8-bit data is valid, which indicates outputting high level
* @n LOW or 0 Parameter level, bit0 in 8-bit data is valid, which indicates outputting low level
* @n 0x00~0xFF If parameter gpioPin is GPIOTotal, bit0-bit7 of parameter level are valid data, corresponding to GPIO0-GPIO7 pins respectively.
*/
void digitalWrite(eGPIOPin_t gpioPin, uint8_t level);
/**
* @fn digitalWrite
* @brief Set the pin to output high and low level, or control to output or stop (interrupt) low level
* @param gpoPin eGPOPin_t enum variable member
* @n eGPO0 General-purpose output pin, GPO0, set output value of pin GPO0
* @n eGPO1 General-purpose output pin, GPO1, set output value of pin GPO1
* @n eGPO2 General-purpose output pin, GPO2, set output value of pin GPO2
* @n eGPO3 General-purpose output pin, GPO3, set output value of pin GPO3
* @n eGPO4 General-purpose output pin, GPO4, set output value of pin GPO4
* @n eGPO5 General-purpose output pin, GPO5, set output value of pin GPO5
* @n eGPO6 General-purpose output pin, GPO6, set output value of pin GPO6
* @n eGPO7 General-purpose output pin, GPO7, set output value of pin GPO7
* @n eGPO8 General-purpose output pin, GPO8, set output value of pin GPO8
* @n eGPO9 General-purpose output pin, GPO9, set output value of pin GPO9
* @n eGPO10 General-purpose output pin, GPO10, set output value of pin GPO10
* @n eGPO11 General-purpose output pin, GPO11, set output value of pin GPO11
* @n eGPO12 General-purpose output pin, GPO12, set output value of pin GPO12
* @n eGPO13 General-purpose output pin, GPO13, set output value of pin GPO13
* @n eGPO14 General-purpose output pin, GPO14, set output value of pin GPO14
* @n eGPO15 General-purpose output pin, GPO15, set output value of pin GPO15
* @n eGPOTotal Set all pins in GPO0~15. When using this parameter,bit0~bit7 of parameter 8bit data bit0~bit7 corresponds to output value of GPO0~GPO7 or GPO8~GPO15
* @param level Output level, output low level or stop
* @n HIGH or 1 When GPO pin group is set to push-pull output mode, output high; for open-drain mode, output low level
* @n LOW or 0 When GPO pin group is set to push-pull output mode, output low, for open-drain mode, no signal output
* @n 0x00~0xFF When gpoPin parameter is eGPOTotal, bit0~bit7 of level are all valid data, corresponding to pin GPO0~GPO7 or GPO8~GPO15
*/
void digitalWrite(eGPOPin_t gpoPin, uint8_t level);
/**
* @fn digitalWrite
* @brief Set IO output value by group
* @param group Group pin, ePinGroup_t enum variable member
* @n eGPIO GPIO pin 0~7, when setting this value, parameter level low 8bits are valid, bit0~bit7 correspond to output of pin GPIO0~GPIO7, indicating setting output value of pin 0~7 in GPIO group.
* @n eGPO GPO pin 0~15, when setting this value, parameter level 16bits are valid, bit0~bit15 correspond to output of pin GPO0~GPIO15, indicating setting output value of pin 0~15 in GPO group.
* @n eGPO0_7 GPO pin 0~7, when setting this value, parameter level low 8bits are valid, bit0~bit7 correspond to output of pin GPO0~GPO7, indicating setting output value of pin 0~7 in GPO group.
* @n eGPO8_15 GPO pin 8~15, when setting this value, parameter level high 8bits are valid, bit8~bit15 correspond to output of pin GPO8~GPO15, indicating setting output value of pin 8~15 in GPO group
* @param level 16bit data or uGroupValue_t union value. Combining with group parameter to represent the pin value of a group. bit0~bit15 correspond to GPIO0~GPIO7(high 8bits invalid) or GPO0~GPO15
* @n 0x0000~0xFFFF 16bits data, bit0~bit15 have different meanings according to the value of parameter group.
* @note uGroupValue_t union, user can operate this to configure pin values.
*/
void digitalWrite(ePinGroup_t group, uGroupValue_t level);
void digitalWrite(ePinGroup_t group, uint16_t level);
/**
* @fn digitalRead
* @brief Read pin level value of GPIO group
* @param pin GPIO pin, eGPIOPin_t enum variable member
* @n eGPIO0 Bi-directional input/output pin, GPIO0, read level status of pin GPIO0, 0 for low level, 1 for high level
* @n eGPIO1 Bi-directional input/output pin, GPIO1, read level status of pin GPIO1, 0 for low level, 1 for high level
* @n eGPIO2 Bi-directional input/output pin, GPIO2, read level status of pin GPIO2, 0 for low level, 1 for high level
* @n eGPIO3 Bi-directional input/output pin, GPIO3, read level status of pin GPIO3, 0 for low level, 1 for high level
* @n eGPIO4 Bi-directional input/output pin, GPIO4, read level status of pin GPIO4, 0 for low level, 1 for high level
* @n eGPIO5 Bi-directional input/output pin, GPIO5, read level status of pin GPIO5, 0 for low level, 1 for high level
* @n eGPIO6 Bi-directional input/output pin, GPIO6, read level status of pin GPIO6, 0 for low level, 1 for high level
* @n eGPIO7 Bi-directional input/output pin, GPIO7, read level status of pin GPIO7, 0 for low level, 1 for high level
* @n eGPIOTotal Bi-directional input/output pin, GPIO group GPIO0~GPIO7, indicating read level status of all pins in GPIO group, returning of bit0~bit8 represents the level value of pin GPIO0~GPIO7 respectively
* @return Level status value
*/
uint8_t digitalRead(eGPIOPin_t pin);
/**
* @fn attachInterrupt
* @brief Set the external interrupt mode and interrupt service function of GPIO pins
* @note Module's pin GPO15 is used to indicate whether an interrupt occurs on GPIO0-GPIO7 in interrupt mode, if an interrupt occurs on a pin, GPO15 will output a low level continuously, otherwise it will output a high level.
* @n When an interrupt occurs on a pin, trigger continuously, if there is interrupt occurring on other pins, level of pin GPO15 keeps low without changing.
* @param gpioPin Pin in GPIO group, eGPIOPin_t enum variable member
* @n eGPIO0 Bi-directional input/output pin, GPIO0, set pin GPIO0 external interrupt mode and interrupt service function
* @n eGPIO1 Bi-directional input/output pin, GPIO1, set pin GPIO1 external interrupt mode and interrupt service function
* @n eGPIO2 Bi-directional input/output pin, GPIO2, set pin GPIO2 external interrupt mode and interrupt service function
* @n eGPIO3 Bi-directional input/output pin, GPIO3, set pin GPIO3 external interrupt mode and interrupt service function
* @n eGPIO4 Bi-directional input/output pin, GPIO4, set pin GPIO4 external interrupt mode and interrupt service function
* @n eGPIO5 Bi-directional input/output pin, GPIO5, set pin GPIO5 external interrupt mode and interrupt service function
* @n eGPIO6 Bi-directional input/output pin, GPIO6, set pin GPIO6 external interrupt mode and interrupt service function
* @n eGPIO7 Bi-directional input/output pin, GPIO7, set pin GPIO7 external interrupt mode and interrupt service function
* @n eGPIOTotal Set the values of all GPIO pins, which indicates setting GPIO0-GPIO7 to the same interrupt mode and interrupt service function
* @param mode Interrupt mode
* @n eLOW Low level interrupt, initial level of the GPIO pin is high, if the GPIO pin detect a low level, pin GPO15 will output a low level signal
* @n eHIGH High level interrupt, initial level of the GPIO pin is low, if the GPIO pin detect a high level, pin GPO15 will output a low level signal
* @n eRISING Rising edge interrupt, initial level of the GPIO pin is low, if the GPIO pin detect a rising edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @n eFALLING Falling edge interrupt, initial level of the GPIO pin is high, if the GPIO pin detect a falling edge, pin GPO15 will output a high-to-low level signal (falling edge)
* @n eCHANGE Double edge interrupt, initial level of the GPIO pin is high. Initial level switches once every time an interrupt occurs. If the GPIO pin detects falling edge or rising edge, pin GPO15 will output a high-to-low level signal(alling edge).
* @note If the any pin level from GPIO0-GPIO7 changes, GPO15 will output a low level signal, and it will not output high level until the levels of all GPIO0-GPIO7 pins return to the initial level.
* @param cb Point to interrupt service function
*/
void attachInterrupt(eGPIOPin_t gpioPin, eInterruptMode_t mode, CH423_INT_CB cb);
Tutorial for Raspberry Pi
Requirements
- Hardware
- Raspberry Pi 4 Model B - 4GB (or similar) x 1
- Gravity: CH423 I2C 24 Digital IO Expansion Module x 1
- M-M/F-M/F-F Jumper wires
- Software
- CH432 python Library
- Download RASPBIAN Official OS
Connection Diagram
- Connect this module to the Raspberry Pi via I2C
Driver Installing
1.Enable Raspberry Pi I2C. Skip this step if it is already enabled. Open terminal and input the following commands and press "Enter":
pi@raspberrypi:~ $ sudo raspi-config
Then use the UP/Down keys to select "5 Interfacing Options", press Enter, select "P5 I2C" and press Enter to comfirm "Yes". Restart the Raspeberry Pi board.
2.To install Python dependency library and git, the Raspberry Pi needs to be networked. Skip this step if already installed. In the terminal, input the following commands and press Enter:
pi@raspberrypi:~ $ sudo apt-get update pi@raspberrypi:~ $ sudo apt-get install build-essential python-dev python-smbus git
3.Download the DFRobot_CH423 driver library. In the terminal, type the following commands and press Enter:
pi@raspberrypi:~ $ cd Desktop/ `pi@raspberrypi:~/Desktop $ git clone https://github.com/DFRobot/DFRobot_CH423
Sample Code
- Sample Code 1 - demo_blink.py
- Sample Code 2 - demo_group.py
- Sample Code 3 - demo_input.py
- Sample Code 4 - demo_interrupt.py
- Sample Code 5 - demo_sleep.py
- Sample Code 6 - demo_water_lamp.py
Sample Code 1 - demo_blink.py
- In the terminal, input the following command and press Enter to run the sample code:
pi@raspberrypi:~/Desktop $ cd DFRobot_CH423/python/raspberry/examples
pi@raspberrypi:~/Desktop/DFRobot_CH423/python/raspberry/examples/ $ python demo_blink.py
Sample Code 2 - demo_group.py
- In the terminal, input the following command and press Enter to run the sample code:
pi@raspberrypi:~/Desktop $ cd DFRobot_CH423/python/raspberry/examples`
`pi@raspberrypi:~/Desktop/DFRobot_CH423/python/raspberry/examples/ $ python demo_blink.py
Sample Code 3 - demo_input.py
- In the terminal, input the following command and press Enter to run the sample code:
pi@raspberrypi:~/Desktop $ cd DFRobot_CH423/python/raspberry/examples`
`pi@raspberrypi:~/Desktop/DFRobot_CH423/python/raspberry/examples/ $ python demo_input.py
Sample Code 4 - demo_interrupt.py
- In the terminal, input the following command and press Enter to run the sample code:
pi@raspberrypi:~/Desktop $ cd DFRobot_CH423/python/raspberry/examples`
`pi@raspberrypi:~/Desktop/DFRobot_CH423/python/raspberry/examples/ $ python demo_interrupt.py
Sample Code 5 - demo_sleep.py
- In the terminal, input the following command and press Enter to run the sample code:
pi@raspberrypi:~/Desktop $ cd DFRobot_CH423/python/raspberry/examples`
`pi@raspberrypi:~/Desktop/DFRobot_CH423/python/raspberry/examples/ $ python demo_sleep.py
Sample Code 6 - demo_water_lamp.py
- In the terminal, input the following command and press Enter to run the sample code:
pi@raspberrypi:~/Desktop $ cd DFRobot_CH423/python/raspberry/examples`
`pi@raspberrypi:~/Desktop/DFRobot_CH423/python/raspberry/examples/ $ python demo_water_lamp.py
FAQ
For any questions, advice or cool ideas to share, please visit the DFRobot Forum.