Search code examples
arduino-unolcd

Printing the output data of a turbidity sensor with Arduino UNO


I am reading the output in voltage of a turbidity sensor : https://www.dfrobot.com/product-1394.html?tracking=5b603d54411d5 with an Arduino UNO. I want to print the value of volts outage and its NTU (turbidity units) on an LCD screen ADM1602U Sparkfun.

I cant seem to print the dtata correctly on the lCD, it opens and lights up (so I think the wiring is ok) but no data appear.

Here is the code I am using:

#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;
}

I used an analogread just to see if I was getting correct voltage values for the sensor and I am.

Thanks


Solution

  • Ok so I found the answer, the libary used was not appropriate to print on a 16x2 LCD display. The following code worked FYI:

    #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);
    
    }