keyestudio
Share your creative ideas with us here
By Ran | 18 June 2024 | 0 Comments

Button Control Modules with ESP32

Introduction

We did a lot of experiments, and for each one we needed to re-upload the code, so can we achieve different functions through an experiment? In this experiment, we will use an external button module to achieve different functions.

Components Required

img

img

img

img

img

img

ESP32 Board*1

ESP32 Expansion Board*1

Keyestudio DIY Purple LED Module*1

Keyestudio Button Module*1

Keyestudio Rotary Potentiometer*1

Keyestudio Obstacle Avoidance Sensor*1

img

img

img

img

img

img

Keyestudio Line Tracking Sensor*1

Keyestudio DIY Joystick Module*1

Keyestudio HC-SR04 Ultrasonic sensor *1

Keyestudio DIYCommon Cathode RGB Module *1

Keyestudio XHT11 Temperature and Humidity Sensor *1

Keyestudio ADXL345 Acceleration Sensor*1

img

img

img

img

 

 

MicroUSB Cable*1

3PDupont Wire*6

4PDupont Wire*3

5PDupont Wire*1

 

 

Wiring Diagram

Test Code

//**********************************************************************
/*
 * Description : Multiple sensors/modules work together
 * Auther      : http//www.keyestudio.com
*/
#include "xht11.h"
#include "adxl345_io.h"

//ADXL345 sda-->21,scl-->22
adxl345 adxl345(21, 22);

//xht11 to gpio15
xht11 xht(15);

//rgb is connected to 4,0,2
int ledPins[] = {4, 0, 2};    //define red, green, blue led pins
const byte chns[] = {0, 1, 2};        //define the pwm channels
int red, green, blue;

//Rocker module port
int X = 35;
int Y = 34;
int KEY = 32;

//Potentiometer pin is connected to analog port 33
int resPin = 33;

//Trace sensor pin connected to IO port 14
int TrackingPin = 14;

//LED is Connected to GP5
#define PIN_LED   5  // the pin of the LED
#define CHAN    3

//Obstacle avoidance sensor is connected to GP27
int Avoid = 27;

//Ultrasonic sensor port
int Trig = 13;
int Echo = 12;

//Key module port
int button = 23;

int PushCounter = 0;//Store the number of times a key is pressed
int yushu = 0;
unsigned char dht[4] = {0, 0, 0, 0};//Only the first 32 bits of data are received, not the parity bits
bool ir_flag = 1;
float out_X, out_Y, out_Z;

void counter() {
  delay(10);
  ir_flag = 0;
  if (!digitalRead(button)) {
    PushCounter++;
  }
}

void setup() {
  Serial.begin(9600);//Set baud rate to 9600
  pinMode(KEY, INPUT);//Button of remote sensing module
  ledcSetup(CHAN, 1000, 12);
  ledcAttachPin(PIN_LED, CHAN);
  pinMode(button, INPUT);//The key module
  attachInterrupt(digitalPinToInterrupt(button), counter, FALLING);  //External interrupt 0, falling edge fired
  pinMode(Avoid, INPUT);//Obstacle avoidance sensor
  pinMode(Trig, OUTPUT);//Ultrasonic module
  pinMode(Echo, INPUT);
  adxl345.Init();
  for (int i = 0; i < 3; i++) {   //setup the pwm channels,1KHz,8bit
    ledcSetup(chns[i], 1000, 8);
    ledcAttachPin(ledPins[i], chns[i]);
  delay(1000);
 }
}

void loop() {
  yushu = PushCounter % 8;
  if (yushu == 0) {  //The remainder is 0
    yushu_0();  //rgb displays
  } else if (yushu == 1) {  //The remainder is 1
    yushu_1();  //Displays the high and low levels read by the tracking sensor
  } else if (yushu == 2) {  //The remainder is 2
    yushu_2();  //Display temperature and humidity value
  } else if (yushu == 3) {  //The remainder is 3
    yushu_3

Leave a Reply

Your email address will not be published.Required fields are marked. *
Name
E-mail
Content
Verification code