Arduino ESP32

参考:https://blog.csdn.net/finedayforu/article/details/108465009

TSR , threshold)

ESP32专门提供了电容触摸传感器的功能, 共有T0,T2~T9 共 9个touch传感器可用.分别对应引脚4、2、15、13、12、14、27、33、32. 无需设置PinMode

touchRead(pin)

返回值 0~255. 触摸强度
注意: 摸得越瓷实,数值越小

void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
   Serial.printf("touch:%d
",touchRead(4));
}

参数:

  • TSR :中断回调函数, 不能带参数, 不能有返回值。
  • threshold:阈值, 达到该阈值会触发此中断 

void TSR()
{
  Serial.printf("我被按下了!
");
  }
 
void setup()
{
  Serial.begin(9600);
  touchAttachInterrupt(4, TSR , 20);
}
 
void loop()
{
  
}

  

二. 霍尔传感器

ESP32自带霍尔传感器 , 当有磁场靠近时,会显示正负值

hallRead()

三. 外部中断

1. 开启外部中断 attachInterrupt(pin,function,mode);

参数:

  • pin: 外部中断引脚
  • function : 外部中断回调函数
  • mode : 5种外部中断模式, 见下表:
中断触发模式说明
RISING 上升沿触发
FALLING 下降沿触发
CHANGE 电平变化触发
ONLOW 低电平触发
ONHIGH 高电平触发
void func1()
{
  Serial.printf("按键中断触发");
  }
void setup()
{
  Serial.begin(9600);
  attachInterrupt(0,func1,FALLING);
}
 
void loop()
{
  
}

2. 关闭引脚中断 detchInterrupt(pin);

无返回值

四. 时间统计函数

1. 开机至今的毫秒数 millis

millis() 返回值是unsigned long 类型, 大约50天溢出一次

2. 开机至今的微秒数 micros

micros() 返回值是unsigned long 类型, 大约70分钟溢出一次

五. 阻塞延时

时间控制函数
    由于我们接下来的实验程序很多都用到延时函数,那么这里就介绍几个:

delay() ----- 毫秒级延时
delayMicroseconds() ----- 微秒级延时

硬件定时器相关请见第六篇

六. 引脚脉冲信号检测 pulseIn()

pulseIn(pin,state)
pulseIn(pin,state,timeout)

参数:

  • pin : 引脚
  • state : 脉冲类型, 可选高或者低
  • timeout : 超时时间, 默认1秒钟. 单位为微秒, 无符号长整型.

返回值: 脉冲宽度, 单位微秒, 数据类型为无符号长整型. 如果超时返回0

例: 使用SR04超声波测距

 
 

板上接线方式,VCC、trig(控制端)、 echo(接收端)、 out(空脚)、 GND

 
 
#include <Arduino.h>
int distance = 0;
void setup()
{
  Serial.begin(115200);
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
}
 
void loop()
{
  digitalWrite(4, HIGH);
  delayMicroseconds(20);
  digitalWrite(4, LOW);
  distance = pulseIn(18,HIGH)/58;
  Serial.printf("当前距离是:%d cm",distance);
  delay(1000);
原文地址:https://www.cnblogs.com/dengziqi/p/14192629.html