Arduino Uno with DHT22, HC-SR04, Servo, LEDs, Buzzer, LDR, a — Arduino Uno simulation

A project that uses various sensors and actuators with an Arduino Uno, including a DHT22, ultrasonic sensor, servo, LEDs, a buzzer, an LDR, and an I2C LCD.

An interactive Arduino Uno circuit simulation you can run free in your browser on Velxio, by mashiyo272.

Components

Sketch code

#include <Wire.h>
#include <DHT.h>
#include <Servo.h>


#define DHT_PIN 2
#define DHT_TYPE DHT22

#define TRIG_PIN 3
#define ECHO_PIN 4

#define SERVO_PIN 5

#define RED_LED 6
#define GREEN_LED 7

#define BUZZER_PIN 8

#define LDR_PIN A0


#define TEMP_LIMIT 30.0
#define DISTANCE_LIMIT 20.0

const unsigned long buzzerInterval = 500;


#define LCD_ADDRESS 0x27

#define LCD_BACKLIGHT 0x08
#define LCD_ENABLE 0x04
#define LCD_RS 0x01



DHT dht(DHT_PIN, DHT_TYPE);
Servo servo;


bool buzzerState = false;
unsigned long lastBuzzerTime = 0;



void lcdWrite4Bits(byte value)
{
  Wire.beginTransmission(LCD_ADDRESS);
  Wire.write(value | LCD_BACKLIGHT);
  Wire.endTransmission();

  delayMicroseconds(1);

  Wire.beginTransmission(LCD_ADDRESS);
  Wire.write(value | LCD_BACKLIGHT | LCD_ENABLE);
  Wire.endTransmission();

  delayMicroseconds(1);

  Wire.beginTransmission(LCD_ADDRESS);
  Wire.write(value | LCD_BACKLIGHT);
  Wire.endTransmission();

  delayMicroseconds(50);
}


void lcdSend(byte value, byte mode)
{
  byte highNibble = value & 0xF0;
  byte lowNibble = (value << 4) & 0xF0;

  lcdWrite4Bits(highNibble | mode);
  lcdWrite4Bits(lowNibble | mode);
}


void lcdCommand(byte command)
{
  lcdSend(command, 0);
}


void lcdData(byte data)
{
  lcdSend(data, LCD_RS);
}


void lcdClear()
{
  lcdCommand(0x01);
  delay(2);
}


void lcdSetCursor(byte column, byte row)
{
  byte address;

  if (row == 0)
    address = 0x00;
  else
    address = 0x40;

  lcdCommand(0x80 | (column + address));
}


void lcdPrint(const char *text)
{
  while (*text)
  {
    lcdData(*text);
    text++;
  }
}


void lcdPrintNumber(int number)
{
  char buffer[12];

  sprintf(buffer, "%d", number);

  lcdPrint(buffer);
}


void lcdPrintFloat(float number, int decimals)
{
  char buffer[16];

  dtostrf(number, 1, decimals, buffer);

  lcdPrint(buffer);
}


void lcdInit()
{
  Wire.begin();

  delay(50);

  lcdWrite4Bits(0x30);
  delay(5);

  lcdWrite4Bits(0x30);
  delayMicroseconds(150);

  lcdWrite4Bits(0x30);
  delayMicroseconds(150);

  lcdWrite4Bits(0x20);

  lcdCommand(0x28);  
  lcdCommand(0x0C);  
  lcdCommand(0x06);   
  lcdClear();
}


float getDistance()
{
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);

  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000);

  if (duration == 0)
  {
    return 999;
  }

  float distance = duration * 0.0343 / 2;

  return distance;
}



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


  dht.begin();

 
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);


  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);


  pinMode(BUZZER_PIN, OUTPUT);


  noTone(BUZZER_PIN);
  digitalWrite(BUZZER_PIN, LOW);


  servo.attach(SERVO_PIN);
  servo.write(0);

  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, HIGH);


  lcdInit();

  lcdSetCursor(0, 0);
  lcdPrint("SMART MONITOR");

  lcdSetCursor(0, 1);
  lcdPrint("Initializing...");

  delay(2000);

  lcdClear();
}



v

More projects by mashiyo272