arduino-电压表##
arduino-电压表测量10V电压,R1=100K,R2=10K
arduino-电压表测量3V电压
/*
A5--SCL
A4--SDA
R1=100K
R2=10K
A0读取模拟值是串联电路R2两端电压,故R1、R2两端电压是模拟值的11倍左右(电阻误差导致)
*/
/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
//取16X16汉字字模 逐行式 顺向高位在前
static const unsigned char PROGMEM str1[] =
{
0x00,0x04,0x27,0xC4,0x14,0x44,0x14,0x54,0x85,0x54,0x45,0x54,0x45,0x54,0x15,0x54,
0x15,0x54,0x25,0x54,0xE5,0x54,0x21,0x04,0x22,0x84,0x22,0x44,0x24,0x14,0x08,0x08
};/*"测",0*/
static const unsigned char PROGMEM str2[] =
{
0x00,0x00,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0xFF,0xFE,0x00,0x00,0x1F,0xF0,
0x11,0x10,0x1F,0xF0,0x11,0x10,0x1F,0xF0,0x01,0x00,0x1F,0xF0,0x01,0x00,0x7F,0xFC
};/*"量",1*/
static const unsigned char PROGMEM str3[] =
{
0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,
0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x0A,0x01,0x02,0x01,0x02,0x00,0xFE
};/*"电",2*/
static const unsigned char PROGMEM str4[] =
{
0x00,0x00,0x3F,0xFE,0x20,0x00,0x20,0x80,0x20,0x80,0x20,0x80,0x20,0x80,0x2F,0xFC,
0x20,0x80,0x20,0x80,0x20,0x90,0x20,0x88,0x20,0x88,0x40,0x80,0x5F,0xFE,0x80,0x00
};/*"压",3*/
int pin = A0; //定义引脚为A0
float n = 0.00; //Vin = 0.00;
int m = 0; //val = 0;
void setup()
{
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextColor(WHITE); //开像素点发光
display.clearDisplay(); //清屏
pinMode(pin,INPUT);
display.setTextSize(1); //设置字体大小
delay(1000);
Serial.println("begin");
display.setTextSize(1); //设置字体大小
display.setCursor(35, 5); //设置显示位置
display.println("-2021 TI-"); //输出字符
display.drawBitmap(32, 15, str1, 16, 16, 1); //画出字符对应点阵数据
display.drawBitmap(48, 15, str2, 16, 16, 1);
display.drawBitmap(64, 15, str3, 16, 16, 1);
display.drawBitmap(80, 15, str4, 16, 16, 1);
display.display();//开显示
}
void loop()
{
m = analogRead(A0); //reads the analog input
n=m*5.00/1024*11.35; //R1、R2两端电压是模拟值的11倍左右(电阻误差导致)
if (n<0.09) //condition
{
n=0.00; //statement to quash undesired reading !
}
Serial.print("voltage:");
Serial.print(n);
Serial.println("V");
delay(2000);
display.clearDisplay(); //清屏
display.setCursor(35, 0); //设置显示位置
display.setTextSize(1); //设置字体大小
display.setTextColor(WHITE); //设置字体颜色白色
display.println("-2021 TI-"); //输出字符
display.setCursor(20,15); //设置字体的起始位置
display.setTextSize(2); //设置字体大小
display.setTextColor(WHITE); //设置字体颜色白色
display.print("voltage:");
display.setCursor(20,40); //设置字体的起始位置
display.setTextSize(2); //设置字体大小
display.setTextColor(WHITE); //设置字体颜色白色
display.print(n);
display.println("v");
display.display();//开显示
}