1_BME280
1.1_BME280+OLED_I2C_0.96(SSD1306)
安装库
Adafruit-GFX
Adafruit_SSD1306
Adafruit_Sensor
Adafruit_BME280
SSD1306引脚连接
SCL=22
SDA=21
BME280引脚连接
SCL=22
SDA=21
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_BME280 bme280;
// 温度字模, 32x16px
const unsigned char str1 [] PROGMEM = {
0x00, 0x00, 0x00, 0x80, 0x33, 0xfc, 0x00, 0x80, 0x1a, 0x04, 0x3f, 0xfe, 0x02, 0x04, 0x20, 0x00,
0x03, 0xfc, 0x22, 0x10, 0x42, 0x04, 0x3f, 0xfe, 0x33, 0xfc, 0x22, 0x10, 0x00, 0x00, 0x22, 0x10,
0x00, 0x00, 0x23, 0xf0, 0x0f, 0xfc, 0x20, 0x00, 0x16, 0x94, 0x2f, 0xfc, 0x16, 0x94, 0x22, 0x08,
0x36, 0x94, 0x21, 0x30, 0x26, 0x94, 0x41, 0xe0, 0x6f, 0xfe, 0x4f, 0xbc, 0x00, 0x00, 0x48, 0x06
};
// 湿度字模, 32x16px
const unsigned char str2 [] PROGMEM = {
0x00, 0x00, 0x00, 0x80, 0x33, 0xfc, 0x00, 0x80, 0x1a, 0x04, 0x3f, 0xfe, 0x02, 0x04, 0x20, 0x00,
0x03, 0xfc, 0x22, 0x10, 0x62, 0x04, 0x3f, 0xfe, 0x13, 0xfc, 0x22, 0x10, 0x00, 0x00, 0x22, 0x10,
0x00, 0x90, 0x23, 0xf0, 0x14, 0x92, 0x20, 0x00, 0x12, 0x94, 0x2f, 0xfc, 0x32, 0x94, 0x22, 0x08,
0x22, 0x98, 0x21, 0x30, 0x20, 0x90, 0x41, 0xe0, 0x4f, 0xfe, 0x4f, 0xbc, 0x00, 0x00, 0x48, 0x06
};
// 气压字模, 32x16px
const unsigned char str3 [] PROGMEM = {
0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x3f, 0xfe, 0x1f, 0xfe, 0x20, 0x00, 0x10, 0x00, 0x20, 0x40,
0x30, 0x00, 0x20, 0x40, 0x2f, 0xfc, 0x20, 0x40, 0x40, 0x00, 0x20, 0xc0, 0x3f, 0xf0, 0x2f, 0xfc,
0x00, 0x10, 0x20, 0x40, 0x00, 0x10, 0x20, 0x50, 0x00, 0x18, 0x20, 0x48, 0x00, 0x18, 0x20, 0x4c,
0x00, 0x08, 0x60, 0x40, 0x00, 0x0a, 0x40, 0xc0, 0x00, 0x0e, 0x5f, 0xfe, 0x00, 0x06, 0x00, 0x00
};
// 海拔字模, 32x16px
const unsigned char str4 [] PROGMEM = {
0x01, 0x00, 0x10, 0x00, 0x31, 0x00, 0x10, 0x88, 0x0b, 0xfe, 0x10, 0x84, 0x02, 0x00, 0x10, 0x80,
0x04, 0x00, 0x7f, 0xfe, 0x05, 0xfc, 0x10, 0x80, 0x63, 0x44, 0x10, 0x80, 0x02, 0x24, 0x10, 0xfc,
0x07, 0xfe, 0x1c, 0xc4, 0x12, 0x44, 0x70, 0xc4, 0x12, 0x44, 0x11, 0x28, 0x32, 0x24, 0x11, 0x38,
0x23, 0xfe, 0x11, 0x10, 0x20, 0x0c, 0x12, 0x38, 0x60, 0x08, 0x16, 0x6c, 0x00, 0x38, 0x74, 0xc2
};
void setup(void)
{
delay(1000);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// 清除显示缓冲区
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
// 初始化BME280传感器
if( bme280.begin(0x76) == 0 )
{ // 连接错误或设备地址错误!
display.setCursor(34, 23);
display.print("Connection");
display.setCursor(49, 33);
display.print("Error");
display.display(); // 更新显示
while(1); // stay here
}
display.drawBitmap(0,0,str1,32,16,1);
display.drawBitmap(0,16,str2,32,16,1);
display.drawBitmap(0,32,str3,32,16,1);
display.drawBitmap(0,48,str4,32,16,1);
display.display();
}
char _buffer[12];
void loop()
{
//从BME280传感器读取温度,湿度,气压,海拔
float temp = bme280.readTemperature(); // 温度
float humi = bme280.readHumidity(); // 湿度
float pres = bme280.readPressure(); // 气压
float altitude =bme280.readAltitude(SEALEVELPRESSURE_HPA); //海拔
// 在SSD1306上打印所有数据
// 1: 打印温度
if(temp < 0){
sprintf(_buffer, "-%02u.%02u C", (int)abs(temp), (int)(abs(temp) * 100) % 100 );}
else{
sprintf(_buffer, "%02u.%02u C", (int)temp, (int)(temp * 100) % 100 );}
display.setCursor(32,4);
display.print(_buffer);
// 2: 打印湿度
sprintf(_buffer, "%02u.%02u %%", (int)humi, (int)(humi * 100) % 100 );
display.setCursor(32,20);
display.print(_buffer);
// 3: 打印气压
sprintf(_buffer, "%04u.%02u hPa", (int)(pres/100), (int)((uint32_t)pres % 100) );
display.setCursor(32,36);
display.print(_buffer);
// 4: 打印海拔
if(altitude < 0){
sprintf(_buffer, "-%02u.%02u m", (int)abs(altitude), (int)(abs(altitude) * 100) % 100 );}
else{
sprintf(_buffer, "%02u.%02u m", (int)altitude, (int)(altitude * 100) % 100 );}
display.setCursor(32,52);
display.print(_buffer);
// 更新显示
display.display();
delay(5000); // 延时
}
2_步进电机(驱动ic:ULN2003)
安装库
Stepper
#include <Stepper.h>
#define STEPS 2038 // the number of steps in one revolution of your motor (28BYJ-48)
Stepper stepper(STEPS, 2, 4, 5, 15);
void setup() {
// nothing to do
}
void loop() {
stepper.setSpeed(1); // 1 rpm
stepper.step(2038); // do 2038 steps -- corresponds to one revolution in one minute
delay(1000); // wait for one second
stepper.setSpeed(6); // 6 rpm
stepper.step(-2038); // do 2038 steps in the other direction with faster speed -- corresponds to one revolution in 10 seconds
}
3_舵机
3.1_SG90(0~180)
VCC(红线) - 5V
SIG(黄色/橙色) - 2
GND(黑色/棕色) - GND
#define COUNT_LOW 0
#define COUNT_HIGH 8888
#define TIMER_WIDTH 16
#include<Arduino.h>
#include "esp32-hal-ledc.h"
void setup() {
ledcSetup(1, 50, TIMER_WIDTH); // channel 1, 50 Hz, 16-bit width
ledcAttachPin(2, 1);
}
void loop() {
for (int i=COUNT_LOW ; i < COUNT_HIGH ; i=i+100)
{
ledcWrite(1, i); // sweep servo 1
delay(50);
}
}