测试一、C5连接oled屏幕、温湿度传感器
连接示意图:

代码
#include <Arduino.h>
#include <Wire.h>
#include "SSD1306Wire.h"
#include <dht11.h> // 添加DHT传感器库
dht11 DHT;
// 引脚定义
#define SDA 9
#define SCL 10
#define DHT_PIN 2 // DHT11连接到2号引脚
// 初始化OLED显示
SSD1306Wire display(0x3c, SDA, SCL);
// 温湿度变量
float temperature = 0;
float humidity = 0;
void setup() {
Serial.begin(9600);
// 初始化OLED
display.init();
display.setContrast(255);
display.setFont(ArialMT_Plain_16);
display.flipScreenVertically(); // 可选:根据安装方向调整
// 初始化I2C引脚
Wire.setPins(SDA, SCL);
Wire.begin();
// 显示启动信息
display.clear();
display.drawString(0, 0, "NSFZ JNFX");
display.drawString(0, 16, "Initializing...");
display.display();
delay(2000); // 给传感器预热时间
Serial.println("DHT11 Sensor Test");
Serial.println("=================");
}
void loop() {
int chk;
chk = DHT.read(DHT_PIN);
// 读取DHT11传感器数据
temperature = DHT.temperature; // 读取温度(摄氏度)
humidity = DHT.humidity; // 读取湿度
// 检查读取是否成功
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
display.clear();
display.drawString(0, 0, "NSFZ JNFX");
display.drawString(0, 16, "Sensor Error!");
display.drawString(0, 32, "Check DHT11");
display.display();
delay(2000);
return;
}
// 格式化显示字符串
String tempStr = String(temperature, 1) + "°C";
String humStr = String(humidity, 1) + "%";
// 清屏并显示数据
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
// 显示标题
display.drawString(0, 0, "NSFZ JNFX");
// 显示温度
display.drawString(0, 16, "Temp:");
display.drawString(50, 16, tempStr);
// 显示湿度
display.drawString(0, 32, "Humidity:");
display.drawString(80, 32, humStr);
// 更新显示
display.display();
// 串口输出调试信息
Serial.printf("Temperature: %.1f°C\n", temperature);
Serial.printf("Humidity: %.1f%%\n", humidity);
Serial.println("-------------------");
// DHT11需要至少2秒的读取间隔
delay(2000);
}
测试结果:
数据正常显示。

测试二、C5连接oled屏幕、模拟tds传感器
连接示意图:

代码
#include <Arduino.h>
#include <Wire.h>
#include "SSD1306Wire.h"
#include <dht11.h> // 添加DHT传感器库
#include <EEPROM.h>
#include "GravityTDS.h"
dht11 DHT;
// 引脚定义
#define SDA 9
#define SCL 10
#define DHT_PIN 3 // DHT11连接到2号引脚
#define TdsSensorPin 2
GravityTDS gravityTds;
float tdsValue = 0;
// 初始化OLED显示
SSD1306Wire display(0x3c, SDA, SCL);
// 温湿度变量
float temperature = 0;
float humidity = 0;
void setup() {
Serial.begin(9600);
// 初始化OLED
display.init();
display.setContrast(255);
display.setFont(ArialMT_Plain_16);
display.flipScreenVertically(); // 可选:根据安装方向调整
// 初始化I2C引脚
Wire.setPins(SDA, SCL);
Wire.begin();
// 显示启动信息
display.clear();
display.drawString(0, 0, "NSFZ JNFX");
display.drawString(0, 16, "Initializing...");
display.display();
delay(2000); // 给传感器预热时间
Serial.println("DHT11 Sensor Test");
Serial.println("=================");
gravityTds.setPin(TdsSensorPin);
gravityTds.setAref(3.3); //reference voltage on ADC, default 5.0V on Arduino UNO
gravityTds.setAdcRange(4096); //1024 for 10bit ADC;4096 for 12bit ADC
gravityTds.begin(); //initialization
}
void loop() {
analogWrite(5, 1024);
int chk;
chk = DHT.read(DHT_PIN);
// 读取DHT11传感器数据
temperature = DHT.temperature; // 读取温度(摄氏度)
humidity = DHT.humidity; // 读取湿度
gravityTds.setTemperature(25); // set the temperature and execute temperature compensation
gravityTds.update(); //sample and calculate
tdsValue = gravityTds.getTdsValue(); // then get the value
Serial.print(tdsValue, 0);
Serial.println("ppm");
// 检查读取是否成功
if (isnan(temperature) || isnan(humidity)|| isnan(tdsValue)) {
Serial.println("Failed to read from DHT sensor!");
display.clear();
display.drawString(0, 0, "NSFZ JNFX");
display.drawString(0, 16, "Sensor Error!");
display.drawString(0, 32, "Check DHT11");
display.drawString(0, 48, "Check tds");
display.display();
delay(2000);
return;
}
// 格式化显示字符串
String tempStr = String(temperature, 1) + "°C";
String humStr = String(humidity, 1) + "%";
String tdsStr = String(tdsValue, 1) ;
// 清屏并显示数据
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
// 显示标题
display.drawString(0, 0, "NSFZ JNFX");
// 显示温度
display.drawString(0, 16, "Temp:");
display.drawString(50, 16, tempStr);
// 显示湿度
display.drawString(0, 32, "Humidity:");
display.drawString(80, 32, humStr);
display.drawString(0, 48, "tds:");
display.drawString(40, 48, tdsStr);
// 更新显示
display.display();
// 串口输出调试信息
Serial.printf("Temperature: %.1f°C\n", temperature);
Serial.printf("Humidity: %.1f%%\n", humidity);
Serial.printf("tds: %.1f%%\n", tdsValue);
Serial.println("-------------------");
// DHT11需要至少2秒的读取间隔
delay(2000);
}
测试结果:
数据显示始终为0,数据异常。
测试三、UNO连接模拟tds传感器
连接示意图:


代码
#define TdsSensorPin A0 //analog pin of UNO R3
#define VREF 5.0 // analog reference voltage(Volt) of the UNO R3 ADC
#define ADCRes 1024 //ADC resolution of UNO R3
#define SCOUNT 30 // sum of sample point
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0, copyIndex = 0;
float averageVoltage = 0, tdsValue = 0, temperature = 25;
void setup()
{
Serial.begin(115200);
pinMode(TdsSensorPin, INPUT);
}
void loop()
{
static unsigned long analogSampleTimepoint = millis();
if (millis() - analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
analogBufferIndex++;
if (analogBufferIndex == SCOUNT)
analogBufferIndex = 0;
}
static unsigned long printTimepoint = millis();
if (millis() - printTimepoint > 800U)
{
printTimepoint = millis();
for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / ADCRes; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation
tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5; //convert voltage value to tds value
//Serial.print("voltage:");
//Serial.print(averageVoltage,2);
//Serial.print("V ");
Serial.print("TDS Value:");
Serial.print(tdsValue, 0);
Serial.println("ppm");
}
}
int getMedianNum(int bArray[], int iFilterLen)
{
int bTab[iFilterLen];
for (byte i = 0; i < iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++)
{
for (i = 0; i < iFilterLen - j - 1; i++)
{
if (bTab[i] > bTab[i + 1])
{
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0)
bTemp = bTab[(iFilterLen - 1) / 2];
else
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
return bTemp;
}
测试结果:
数据正常显示,串口数据如下图

评论