marți, 26 aprilie 2022

Arduino Thermometer with RTC Clock

  This is an update of the previous project TM1637 4-Digit Display Clock Arduino Project . I added a temperature & humidity sensor DHT11 or DHT22.

Schematics:

Source code:

/* https://arduinogeeks.blogspot.com */
/* Degital 24 hour time format clock by Arduino, TM1637 4 digit 7 segment display and DS32321 RTC.*/
// Add libraries: RTClib and TM1637
#include "RTClib.h"
#include <TM1637Display.h>
// Define the connections pins for TM1637 4 digit 7 segment display
#define CLK 8
#define DIO 9
// Create rtc and display object
RTC_DS3231 rtc;
TM1637Display display = TM1637Display(CLK, DIO);
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Define the connections pins
#define CLK 8
#define DIO 9
#define DHTPIN 5
// Create variable
int temperature_celsius;
int temperature_fahrenheit;
int humidity_read;
// Create °C symbol
const uint8_t celsius[] = {
SEG_A | SEG_B | SEG_F | SEG_G, // Circle
SEG_A | SEG_D | SEG_E | SEG_F // C
};
// Create °F symbol
const uint8_t fahrenheit[] = {
SEG_A | SEG_B | SEG_F | SEG_G, // Circle
SEG_A | SEG_E | SEG_F | SEG_G // F
};
// Create H symbol
const uint8_t humidity[] = {
SEG_B | SEG_C | SEG_G | SEG_E | SEG_F // H
};
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Create dht object of type DHT:
DHT dht = DHT(DHTPIN, DHTTYPE);
void setup() {
// Begin serial communication at a baud rate of 9600
Serial.begin(9600);
// Wait for console opening
delay(3000);
// Check if RTC is connected correctly
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power and if so, set the time:
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// The following line sets the RTC to the date & time this sketch was compiled:
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
rtc.adjust(DateTime(2022,4, 20, 9, 57, 0));
}
// Setup sensor
dht.begin();
// Set the display brightness (0-7):
display.setBrightness(5);
// Clear the display:
display.clear();
}
void loop() {
// Get current date and time
DateTime now = rtc.now();
// Create time format to display:
int displaytime = (now.hour() * 100) + now.minute();
// Print displaytime to the Serial Monitor
Serial.println(displaytime);
// Display the current time in 24 hour format with leading zeros enabled and a center colon:
display.showNumberDecEx(displaytime, 0b11100000, true);
// Remove the following lines of code if you want a static instead of a blinking center colon:
//delay(1000);
//display.showNumberDec(displaytime, true); // Prints displaytime without center colon.
delay(5000);
// Read the temperature as Celsius and Fahrenheit
temperature_celsius = dht.readTemperature();
temperature_fahrenheit = dht.readTemperature(true);
humidity_read = dht.readHumidity();
display.clear();
// Display the temperature in celsius format
display.showNumberDec(temperature_celsius, false, 2, 0);
display.setSegments(celsius, 2, 2);
delay(2000);
display.clear();
// Display the temperature in fahrenheit format
display.showNumberDec(temperature_fahrenheit, false, 2, 0);
display.setSegments(fahrenheit, 2, 2);
delay(2000);
display.clear();
// Display the humidity
// Display the humidity
display.showNumberDec(humidity_read, false, 2, 0);
display.setSegments(humidity, 1, 3);
delay(2000);
display.clear();
}

Simple Arduino Project with DHT11/22 sensor and TM1637 LCD

 This is a simple Arduino project based on DHT11 or DHT22 temperature&humidity sensor and TM1637 4 digit 7 segment LCD.



 

// Include the libraries
#include <TM1637Display.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Define the connections pins
#define CLK 8
#define DIO 9
#define DHTPIN 5
// Create variable
int temperature_celsius;
int temperature_fahrenheit;
int humidity_read;
// Create °C symbol
const uint8_t celsius[] = {
SEG_A | SEG_B | SEG_F | SEG_G, // Circle
SEG_A | SEG_D | SEG_E | SEG_F // C
};
// Create °F symbol
const uint8_t fahrenheit[] = {
SEG_A | SEG_B | SEG_F | SEG_G, // Circle
SEG_A | SEG_E | SEG_F | SEG_G // F
};
// Create H symbol
const uint8_t humidity[] = {
SEG_B | SEG_C | SEG_G | SEG_E | SEG_F // H
};
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Create display object of type TM1637Display
TM1637Display display = TM1637Display(CLK, DIO);
// Create dht object of type DHT:
DHT dht = DHT(DHTPIN, DHTTYPE);
void setup() {
// Set the display brightness (0-7)
display.setBrightness(5);
// Clear the display
display.clear();
// Setup sensor
dht.begin();
}
void loop() {
// Read the temperature as Celsius and Fahrenheit
temperature_celsius = dht.readTemperature();
temperature_fahrenheit = dht.readTemperature(true);
humidity_read = dht.readHumidity();
// Display the temperature in celsius format
display.showNumberDec(temperature_celsius, false, 2, 0);
display.setSegments(celsius, 2, 2);
delay(2000);
display.clear();
// Display the temperature in fahrenheit format
display.showNumberDec(temperature_fahrenheit, false, 2, 0);
display.setSegments(fahrenheit, 2, 2);
delay(2000);
display.clear();
// Display the humidity
display.showNumberDec(humidity_read, false, 2, 0);
display.setSegments(humidity, 1, 3);
delay(2000);
display.clear();
}

Ultrasonic Sensor HC-SR04 Arduino Project

 

The HC-SR04 is an affordable and easy to use distance measuring sensor which has a range from 2cm to 400cm (about an inch to 13 feet).

The sensor is composed of two ultrasonic transducers. One is transmitter which outputs ultrasonic sound pulses and the other is receiver which listens for reflected waves.

How the HC-SR04 Ultrasonic Distance Sensor Works?

It emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.

For that purpose we are using the following basic formula for calculating distance:

Distance = Speed x Time

The time is the amount of time the Echo pin was HIGH, and the speed is the speed of sound which is 340m/s. There’s one additional step we need to do, and that’s divide the end result by 2. and that’s because we are measuring the duration the sound wave needs to travel to the object and bounce back.

How to Connect HC-SR04 Ultrasonic Sensor to Arduino

Here’s how we need to connect the HC-SR04 sensor to an Arduino board.



 

HC-SR04 Ultrasonic Sensor Arduino Code

Here’s a code for measuring distance using the HC-SR04 ultrasonic sensor and Arduino.

/*
Ultrasonic HC-SR04 Sensor Simple Arduino Project
https://arduinogeeks.blogspot.com
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int trigPin = 10;
const int echoPin = 9;
long duration;
int distanceCm, distanceInch;
void setup() {
//lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
lcd.init();
lcd.backlight();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm = duration * 0.034 / 2;
distanceInch = duration * 0.0133 / 2;
lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm");
delay(10);
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print(" inch");
delay(10);
}

luni, 18 aprilie 2022

TM1637 4-Digit Display Clock Arduino Project

 This project shows you how to make a digital clock using a TM1637 4-digit 7-segment display with Arduino.


TM1637 4-Digit Display Clock Source Code

// arduinogeeks.blogspot.com
// Digital 24 hour time format clock by Arduino, TM1637 4 digit 7 segment display and DS32321 RTC
// Add libraries: RTClib and TM1637
#include "RTClib.h"
#include <TM1637Display.h>
// Define the connections pins for TM1637 4 digit 7 segment display
#define CLK 8
#define DIO 9
// Create rtc and display object
RTC_DS3231 rtc;
TM1637Display display = TM1637Display(CLK, DIO);
void setup() {
// Begin serial communication at a baud rate of 9600
Serial.begin(9600);
// Wait for console opening
delay(3000);
// Check if RTC is connected correctly
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power and if so, set the time:
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// The following line sets the RTC to the date & time this sketch was compiled:
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// Set the display brightness (0-7):
display.setBrightness(5);
// Clear the display:
display.clear();
}
void loop() {
// Get current date and time
DateTime now = rtc.now();
// Create time format to display:
int displaytime = (now.hour() * 100) + now.minute();
// Print displaytime to the Serial Monitor
Serial.println(displaytime);
// Display the current time in 24 hour format with leading zeros enabled and a center colon:
display.showNumberDecEx(displaytime, 0b11100000, true);
// Remove the following lines of code if you want a static instead of a blinking center colon:
delay(1000);
display.showNumberDec(displaytime, true); // Prints displaytime without center colon.
delay(1000);
}
view raw tm1637-rtc.ino hosted with ❤ by GitHub