POUWIEL|COM

JeroenPouwiel

ARDUINO: lcd + IR sensor

//source: @https://www.instructables.com/id/Simple-Motor-Speed-Tester-Tachometer/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display

const int dataIN = 3; //IR sensor INPUT

unsigned long prevmillis; // To store time
unsigned long duration; // To store time difference
unsigned long lcdrefresh; // To store time for lcd to refresh

int rpm; // RPM value

boolean currentstate; // Current state of IR input scan
boolean prevstate; // State of IR sensor in previous scan

void setup()
{
  pinMode(dataIN,INPUT);
  lcd.init();
  lcd.backlight();
  prevmillis = 0;
  prevstate = LOW;
}

void loop()
{
 // RPM Measurement
 currentstate = digitalRead(dataIN); // Read IR sensor state
 if( prevstate != currentstate) // If there is change in input
   {
     if( currentstate == HIGH ) // If input only changes from LOW to HIGH
       {
         duration = ( micros() - prevmillis ); // Time difference between revolution in microsecond
         rpm = (60000000/duration); // rpm = (1/ time millis)*1000*1000*60;
         prevmillis = micros(); // store time for next revolution calculation
       }
   }
  prevstate = currentstate; // store this scan (prev scan) data for next scan
  
  // LCD Display
  if( ( millis()-lcdrefresh ) >= 100 )
    {
      lcd.setCursor(0,0);
      lcd.print("Speed of Motor");
      lcd.setCursor(0,1);
      lcd.print("RPM = ");
      lcd.print(rpm);
      lcd.print("   ");
      lcdrefresh = millis();   
    }

}

ARDUINO: Sonar + humidity + lcd


#include "DHT.h";
#include "NewPing.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display

// Define Constants
#define DHTPIN 7        // DHT-22 Output Pin connection
#define DHTTYPE DHT11   // DHT Type is DHT 22 (AM2302)
#define TRIGGER_PIN  10
#define ECHO_PIN     13
#define MAX_DISTANCE 400

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

// Define Variables
float hum;    // Stores humidity value in percent
float temp;   // Stores temperature value in Celcius
float duration; // Stores HC-SR04 pulse duration value
float distance; // Stores calculated distance in cm
float soundsp;  // Stores calculated speed of sound in M/S
float soundcm;  // Stores calculated speed of sound in cm/ms
int iterations = 5;
int humint = 0;


// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE); 

void setup() {
  lcd.init();
  lcd.backlight();
  dht.begin();
}

void loop()
{

  delay(2000);  // Delay so DHT-22 sensor can stabalize
   
    hum = dht.readHumidity();  // Get Humidity value
    humint = hum;
    temp= dht.readTemperature();  // Get Temperature value
    
    // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/ms
    
    soundcm = soundsp / 10000;
    
  duration = sonar.ping_median(iterations);
  
  // Calculate the distance
  distance = (duration / 2) * soundcm;
  
  // Send results to Serial Monitor
    lcd.setCursor(1,0);
    //lcd.print("Sound:");
    lcd.print(soundsp);
    lcd.print("m/s, ");
    //lcd.print("Hum:");
    lcd.print(humint);
    lcd.print("%");
    lcd.setCursor(1,1);
    lcd.print(temp);
    lcd.print("C, ");
    //lcd.print("Dis:");

    if (distance >= 400 || distance <= 2) {
      lcd.clear();
      lcd.setCursor(1,0);
      lcd.print("Distance:");
      lcd.setCursor(1,1);
      lcd.print("Out of range");
    }
    else {
      lcd.print(distance,2);
      lcd.print("cm");
    delay(500);
    }
}

Categories