I am just starting with Arduino and I made an sketch from several others and included some code of my own and I want to know what the mistakes I made on the code / electronics.
The code starts with the connections and is all commented, as I intend to publish it to other begginers.
/* Connections
******
BUZZER:
NEGATIVE PIN: GND
POSITIVE PIN: PWM6
******
DHT 11
WITH THE HOLES FACING YOU
PIN 1 5V
PIN 2 A0
PIN 3 EMPTY
PIN 4 GND
***********
Potentiometer facing you
left: GND
MIDDLE: LCD PIN 3
RIGHT: 5V
***********
Photoresistor
One leg to 5V
The other leg to A2 and 10k resistor (brown, black, orange) to GND
***********
LCD pin - Arduino Pin
Pin 1 (VSS) - GND
Pin 2 (VDD) - 5V
Pin 3 V0 - POTENTIOMETER 10K OHMS CENTRAL PIN
Pin 4 RS - PWM12
Pin 5 R/W - GND
Pin 6 E - PWM11
Pin 11 D-4 - PWM5
Pin 12 D-5 - PWM4
Pin 13 D-6 - PWM3
Pin 14 D-7 - PWM2
Pin 15 A (BACKLIGHT) - 220 OHM (RED RED BROWN) - A1 // The lcd backlight I connected to pin A1, so it can be turned off
Pin 16 K (BACKLIGHT) - GND
THE RESISTOR ON PIN 15 MAKES THE LCD DRAW LESS POWER FROM ARDUINO
*/
// INCLUDING LIBRARIES FOR 16X2 LCD, DHT and time
#include <TimeLib.h>
#include <LiquidCrystal.h>
#include <dht.h>
#define dht_dpin A0 // THIS NEXT PINS CAN BE CHANGED IF NEEDED.
#define bklt A1
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Calls the LCD library and set the pins
dht DHT; // Names the dht library DHT
int freq; // Creates a variable named freq.
int ledPin = 13;
int cic = 0; // Set the counter to zero
// Photosensor
int PhotoPin = A2; // select the input pin for the sensor
int PhotoValue = 0; // variable to store the value coming from the sensor
int minLight; //Used to calibrate the readings
int maxLight; //Used to calibrate the readings
int lightLevel;
int adjustedLightLevel;
int off = 0;
void setup() {
pinMode(ledPin, OUTPUT); // Makes the pin 13 output
digitalWrite(ledPin, LOW); // Turn off the board led attached to pin 13 off.
pinMode(bklt, OUTPUT); // Makes the Backlight switchable
digitalWrite(bklt, HIGH); // Turns the Backlight On
lcd.begin(16, 2); // Starts the LCD
lcd.clear(); // Clears the LCD
lcd.display(); // Turns the LCD on
lcd.setCursor(0, 0); // Puts the cursor on the first digit of the first line
lcd.print("Temperature & Hu"); // Writes the first line
lcd.setCursor(0, 1); // Puts the cursor on the first digit of the second line
lcd.print("midity Sensors"); // Writes the second line
delay(2000); // Wait rest of 1000ms recommended delay before accessing sensor
second(0);
} //end "setup()"
void loop() {
if (lightLevel <= 15) { /* Turns off the backlight if the light read in the previous loop is
too low, so the backlight does not interfere in the next reading. */
digitalWrite(bklt, LOW);
delay(100);
off = 1; // Sets the variable to determine a 100ms shorter final delay
}
if (lightLevel > 15) {
off = 2; // Sets the variable to determine a 2 minutes final delay
}
// auto-adjust the minimum and maximum limits in real time
lightLevel = analogRead(PhotoPin);
if (minLight > lightLevel) {
minLight = lightLevel;
}
if (maxLight < lightLevel) {
maxLight = lightLevel;
}
digitalWrite(bklt, HIGH);
cic = (cic + 1); // Add 1 to the number of cycles
DHT.read11(dht_dpin); // THIS READING MUST HAVE AT LEAST A 2 SECONDS INTERVAL.
lcd.display(); // Turns the display back on (it is turned off at the end of the loop)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(lightLevel);lcd.print(" "); /* Prints the light measured by the photoresistor.
The A/D converter sets a scale from 1 to 1023. */
lcd.print(DHT.temperature, 0); // Prints the temperature from DHT. The <, 0> instructs to print no decimals
lcd.print((char)223); // Prints the degree sign
lcd.print("C ");
lcd.print(DHT.humidity, 0); // Prints the humidity from DHT. The <, 0> instructs to print no decimals
lcd.print("% ");
//lcd.print("L:");
lcd.setCursor(0, 1);
/*lcd.print("C");*/ lcd.print(cic); //Prints the number of cycles runned since turned on.
lcd.print(" "); lcd.print(day()); lcd.print("d"); lcd.print(hour()); printDigits(minute()); //Prints time since turned on.
if (DHT.temperature >= 32 ) { // Sets a buzzer alarm if the measured temperature is at least 32C
freq = 2000 + DHT.temperature; // Sets the variable frequency
tone(6, freq, 500); // Buzz for half a second
}
if (DHT.humidity < 35 ) { // Sets a buzzer alarm if the measured humidity is bellow 35%
freq = 300; tone(6, freq, 500); // Sets the frequency and plays it for half a second
}
if (off == 1) {
delay(119900); // waits 1.9 minutes to start the loop again. With the 100ms delay on line 89, the total loop interval will be 2 minutes.
}
if (off == 2) {delay(120000);} // Waits 2 minutes to start the loop again.
}// end loop()
void printDigits(int Digits) {
// utility function for digital clock display: prints preceding colon and leading 0
lcd.print("h");
if (Digits < 10)
lcd.print('0');
lcd.print(Digits);
}
DHT11_photo_buzzer_LCD_AlwaysOn.ino (5.03 KB)