【问题标题】:Printing the output data of a turbidity sensor with Arduino UNO使用 Arduino UNO 打印浊度传感器的输出数据
【发布时间】:2019-09-30 21:57:49
【问题描述】:

我正在使用 Arduino UNO 读取浊度传感器的电压输出:https://www.dfrobot.com/product-1394.html?tracking=5b603d54411d5。我想在 LCD 屏幕 ADM1602U Sparkfun 上打印电压中断值及其 NTU(浊度单位)。

我似乎无法在 LCD 上正确打印 dtata,它打开并亮起(所以我认为接线没问题)但没有数据出现。

这是我正在使用的代码:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int sensorPin = A0;
int val = 0;
float volt;
float ntu;

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

  // Turn on the blacklight and print a message.
  lcd.backlight();
}

void loop()
{  
  volt = ((float)analogRead(sensorPin)/1023)*5;
  ntu = -1120.4*square(volt)+5742.3*volt-4353.8; 
  val = analogRead(volt);
  Serial.println(volt); 

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(volt);
  lcd.print(" V");

  lcd.setCursor(0,1);
  lcd.print(ntu);
  lcd.print(" NTU");
  delay(10);
}

float round_to_dp( float in_value, int decimal_place )
{
  float multiplier = powf( 10.0f, decimal_place );
  in_value = roundf( in_value * multiplier ) / multiplier;
  return in_value;
}

我使用模拟读数只是为了查看我是否获得了正确的传感器电压值,我是。

谢谢

【问题讨论】:

    标签: arduino-uno lcd


    【解决方案1】:

    好的,我找到了答案,使用的库不适合在 16x2 LCD 显示器上打印。以下代码仅供参考:

    #include <Wire.h> 
    #include <SoftwareSerial.h>
    
    // Set the LCD address to 0x27 for a 16 chars and 2 line display
    SoftwareSerial LCD(10, 11); // Arduino SS_RX = pin 10 (unused), Arduino SS_TX = pin 11 
    int sensorPin = A0;
    float volt;
    float ntu;
    
    void setup()
    {
      Serial.begin(9600);
      LCD.begin(9600); // set up serial port for 9600 baud
      delay(500); // wait for display to boot 
    }
    
    void loop()
    {  
    
      volt = ((float)analogRead(sensorPin)/1023)*5;
      ntu = -1120.4*square(volt)+5742.3*volt-4352.9; 
      Serial.println(volt); 
    
      // move cursor to beginning of first line
      LCD.write(254); 
      LCD.write(128);
    
      // clear display by sending spaces
      LCD.write("                "); 
      LCD.write("                ");
    
     // move cursor to beginning of first line
      LCD.write(254); 
      LCD.write(128);
    
      LCD.print(volt);
      LCD.write(" V");
    
      LCD.write(254);
      LCD.write(192);
    
      LCD.print(ntu);
      LCD.write(" NTU");
      delay(1000);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      相关资源
      最近更新 更多