Arduino系列之压力传感器的使用以及用OLED显示出来检测到的数据(详细教程)

一、压力传感器单独使用

1.Hx711与Arduino MEGA 2560的接线

Arduino MEGA 2560 -------- Hx711模块
D8 -------------------------------- SCK
D9 -------------------------------- DT
Vcc ------------------------------- Vcc
Gnd ------------------------------ Gnd
D8和D9的接线可以去看库文件HX711.h里面的HX711_SCK和HX711_DT改变后面的数字就可以改变接线,库文件链接如下:

在这里插入图片描述

Hx711模块 ---------------------传感器
E+ ---------------------------------红
E- ----------------------------------黑
A- ----------------------------------白
A+ ---------------------------------绿

2.Hx711与Arduino MEGA 2560的实物连线

压力传感器实物连接图

3.Hx711与Arduino MEGA 2560的代码

#include <HX711.h>
float Weight = 0;

void setup()
{
  Serial.begin(9600);
  Init_Hx711();
  Serial.print("Welcome to use!n");
  Get_Maopi();
  delay(3000);
}


void loop()
{
  delay(100);
  int x = 0;
  Weight = Get_Weight();  //计算放在传感器上的重物重量
//  Serial.print(float(Weight/1000),3); //串口显示重量
//  Serial.print(" kgn");  //显示单位
//  Serial.print("n");   //显示单位
  delay(200);        //延时1s
  
  x = abs(Weight);
  Serial.print(x);
  Serial.print(" gn");  //显示单位
 }

二、OLED的单独使用

1.OLED与Arduino MEGA 2560连线

OLED ------------------ Arduino MEGA 2560
Gnd -------------------- Gnd
Vcc --------------------- 3.3V
SCL -------------------- SCL
SDA -------------------- SDA

2.OLED与Arduino MEGA 2560实物图

OLED显示数字,英文,中文

3.OLED与Arduino MEGA 2560代码,显示数字,英文,中文

//显示中英文字符程序

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define LOGO16_GLCD_HEIGHT 16 //定义显示高度
#define LOGO16_GLCD_WIDTH  16 //定义显示宽度

//中文:物  (存储点阵变量,用函数display.drawBitmap()调用即可)
static const unsigned char PROGMEM str_1[] =
{ 
0x00,0x00,0x0C,0x00,0x0E,0x70,0x0E,0x60,0x3E,0xE0,0x3E,0xC0,0x3F,0xFE,0x6F,0xFE,
0x0E,0xFE,0x1F,0xFE,0x7F,0xEC,0xEF,0xCC,0x0F,0xBC,0x0C,0x18,0x0C,0x00,0x00,0x00,
};

//中文:联
static const unsigned char PROGMEM str_2[] =
{ 
0x00,0x00,0x00,0x00,0x00,0x1C,0x3F,0xD8,0x7E,0xF0,0x36,0x3C,0x3F,0xF8,0x36,0x60,
0x3E,0xFE,0x37,0xF0,0x3E,0x70,0xF6,0xF8,0x07,0xDE,0x0F,0x0F,0x0E,0x00,0x00,0x00,
};

//中文:网
static const unsigned char PROGMEM str_3[] =
{ 
0x00,0x00,0x00,0x00,0x00,0x78,0x3F,0xFC,0x18,0x1C,0x1B,0xFC,0x1B,0x7C,0x1F,0xFC,
0x1F,0xFC,0x1F,0xBC,0x1C,0x1C,0x38,0x1C,0x38,0x3C,0x38,0x3C,0x00,0x18,0x00,0x00,
};

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   
{                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  
  display.clearDisplay(); //清屏

  //英文字符显示,直接用display.println或print显示字符串就行
  //println换行,print不换行
  display.setTextSize(1);             //设置字体大小
  display.setTextColor(WHITE);        //设置字体颜色白色
  display.setCursor(0,0);             //设置字体的起始位置
  display.println("Hello, world!");   //输出字符并换行
  
  display.setTextColor(BLACK, WHITE); //设置字体黑色,字体背景白色 
  display.println(3.141592);          //输出数字并换行
  
  display.setTextSize(2);             //设置字体大小
  display.setTextColor(WHITE);        //设置字体白色
  display.print("0x");                //输出字符
  display.println(0xDEADBEEF, HEX);   //输出为ASCII编码的十六进制
  //display.display();                  //显示以上
  
  //中文字符显示
  display.drawBitmap(26, 32, str_1, 16, 16, 1); //在坐标X:26  Y:16的位置显示中文字符物
  display.drawBitmap(42, 32, str_2, 16, 16, 1); //在坐标X:42  Y:16的位置显示中文字符联
  display.drawBitmap(58, 32, str_3, 16, 16, 1); //在坐标X:26  Y:16的位置显示中文字符网
  display.display();                  //把缓存的都显示
}

void loop() 
{

}

三、OLED与压力传感器一起使用

1.接线跟上面的单独使用是一样的就不重复了

2.实物展示

在这里插入图片描述

3.OLED与重力传感器与Arduino MEGA 2560代码

#include "HX711.h"
float Weight = 0;

//显示中英文字符程序
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
//#define TrigPin 2
//#define EchoPin 3

Adafruit_SSD1306 display(OLED_RESET);
//Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);//声明OLED
//一,二参数0.9寸OLED长款参数为128,64,第三个参数默认&Wire,第四个参数
//设置重置脚,-1就是不设置

void setup()
{
  //压力
  Init_Hx711();//初始化HX711模块连接的IO设置
  Serial.begin(9600);
  Serial.print("Welcome to use!n");
  delay(3000);
  Get_Maopi();    //获取毛皮

  //oled
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.setTextColor(WHITE);//开像素点发光
  display.clearDisplay();//清屏
  display.setTextSize(2);//设置字体大小  
}

void loop()
{
  int dis;
  Weight = Get_Weight();//计算放在传感器上的重物重量
  Serial.print(float(Weight),3);//串口显示重量
  Serial.print(" gn");//显示单位
  Serial.print("n");//显示单位
  delay(1000);

  dis = Weight;
  display.clearDisplay();//清屏
  display.setCursor(50,10);//定位用的
  display.print(dis);
  display.println("g");
  display.drawRect(0, 33, 128, 12, WHITE);//画矩阵来着
  display.fillRect(2, 35, map(dis, 2, 30, 2, 124), 8, WHITE);
  //map(要映射的值,旧区间初值,旧区间终值,新区间初值,新区间终值)
  display.display();//开显示
}

创作不易,请多多支持,你们的支持是我更新的动力,点赞加关注哦

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>