pia-468x60

pia-728x90

Tuesday, June 28, 2022

LED Blinking using TimerOne Library of Arduino

The Arduino Uno is typically powered by the Atmega328. It has Timer/Counter1 module to make a timing or counting process. To access this timer module, the programmer needs to install a library for timer1. This timer library can be used to create a timing delay, or a PWM signal, etc.

Blinking an LED using Timer1 Library of Arduino
Running program on board

In this example, I use this timer library to blinking an LED. This library has a timer interrupt for a specific period. So we can use its timer interrupt to create a timing tick. Using this feature the main program loop doesn't need to wait for any duration. It has an advantage rather than using the Millis(), or delay function library of the Arduino.

  1. #include <TimerOne.h>
  2.  
  3. void setup(){
  4. pinMode(13,OUTPUT);
  5. //set timer 1 duration to 250ms
  6. Timer1.initialize(250000);
  7. //Interrupt Handling
  8. Timer1.attachInterrupt(blinkLed);
  9. }
  10.  
  11. void loop(){
  12.  
  13. }
  14.  
  15. bool state=false;
  16. void blinkLed(){
  17. digitalWrite(13,state);
  18. state^=1;
  19. }

I don't draw its schematic here because this example use only the BUILD-IN LED of the Arduino Uno board. 

Click here to download this example.

Monday, June 27, 2022

Arduino and DS18B20 Temperature Controlling Example

 A temperature controller can be found anywhere in the market at low cost. However we can create it one using an analog circuit, or even a programmable device. Here I create an Arduino-based temperature controller using some components:

  1. An Arduino Uno board
  2. A DS18B20 temperature sensor
  3. A 5V relay module
  4. A character LCD with some other components and accessary.
Arduino and DS18B20 Temperature Controlling Example
Circuit prototyping on breadboard

The controller request and get temperature data from DS18B20 in degree Celsius. The display show either Celsius and degree Fahrenheit. It updates the data for every 5 seconds. I set in program for a condition for the temperature above 30 degree Celsius. Whenever it's true an output relay connects to pin 13 will turn on.

Arduino and DS18B20 Temperature Controlling Example
Schematic Diagram

I use DallasTemperature.h file due to its simplicity. 

  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. #include <LiquidCrystal.h>
  4.  
  5. //LCD Connection RS EN D4...D7
  6. LiquidCrystal lcd(7,8,9,10,11,12);
  7.  
  8. OneWire oneWire(4);
  9.  
  10. DallasTemperature sensors(&oneWire);
  11.  
  12. //Relay Output Pin
  13. const int relay=13;
  14.  
  15. void setup(){
  16. pinMode(relay,OUTPUT);
  17. //Start up the 1-Wire Library
  18. sensors.begin();
  19. lcd.begin(16,2);
  20. lcd.print("ds18B20 Example");
  21. delay(3000);
  22. lcd.clear();
  23. }
  24.  
  25. float getC,getF;
  26.  
  27. void loop(){
  28. //Send the command to get Temperature
  29. sensors.requestTemperatures();
  30. getC=sensors.getTempCByIndex(0);
  31. getF=DallasTemperature::toFahrenheit(getC);
  32.  
  33. if(int(getC)>=30) digitalWrite(relay,HIGH);
  34. else digitalWrite(relay,LOW);
  35.  
  36. lcd.home();
  37. lcd.print("Temperature: ");
  38. lcd.setCursor(0,1);
  39. lcd.print(String(getC,1)+char(223)+"C"+" "+String(getF,1)+char(223)+"F");
  40. delay(5000);
  41. }

Click here to download its source file.


Friday, June 24, 2022

Arduino Uno DS3231 RTC and Character LCD Example

In previous post, I showed an example of using a character LCD with Arduino Uno. Here I will add a Real Time Clock (RTC) module, that it's powered by the DS3231 real time clock chip. This chip contain timing data that store in its internal RAM. Optionally it contain a temperature sensor chip that tell us the current device temperature.

Arduino Uno DS3231 RTC and Character LCD Example
An RTC module that contain a DS3231 and an EEPROM

In this example, the Arduino Uno will read current timing from DS3231. That timing will display on the character LCD.

Arduino Uno DS3231 RTC and Character LCD Example
Program Example

The first line are Date/Month/Year and the device temperature in degree Celsius. The second line are the current time with AM/PM indication.

Arduino Uno DS3231 RTC and Character LCD Example
Schematic

I use PWM of the Arduino to adjust the LCD contrast Vo. The PWM pin 6 of Arduino connects to the LCD Vo pin via a 2.2 kOhm resistor. The PWM duty cycle is 50. 

The Arduino source code is only a little.

  1. //Arduino LCD and DS3231 RTC Example
  2. #include <LiquidCrystal.h>
  3.  
  4. #include <DS3231.h>
  5. #include <Wire.h>
  6.  
  7. LiquidCrystal lcd(7,8,9,10,11,12);
  8. DS3231 Clock;
  9. bool Century = false;
  10. bool h12,PM;
  11. String dut;
  12.  
  13. void setup(){
  14. //LCD Contrast
  15. pinMode(6,OUTPUT);
  16. analogWrite(6,50);
  17. //Start I2C
  18. Wire.begin();
  19. lcd.begin(16,2);
  20. lcd.print("Arduino LCD and");
  21. lcd.setCursor(0,1);
  22. lcd.print("DS3231 Example");
  23. delay(3000);
  24. lcd.clear();
  25. }
  26.  
  27. void loop(){
  28. //Check PM or AM
  29. if(PM) dut="PM";
  30. else dut="AM";
  31.  
  32. //Date
  33. lcd.setCursor(0,0);
  34. lcd.print(String(Clock.getDate(),DEC)+"/"+String(Clock.getMonth(Century),DEC)
  35. +"/"+"20"+String(Clock.getYear(),DEC)+" "+String(Clock.getTemperature(),2));
  36.  
  37. //Time
  38. lcd.setCursor(0,1);
  39. lcd.print(String(Clock.getHour(h12,PM),DEC)+":"+String(Clock.getMinute(),DEC)
  40. +":"+String(Clock.getSecond(),DEC)+" "+dut);
  41. }

Click here to download its source file.

Labels