Timer Switch
Learning Objectives:
After performing this lab exercise, you will be able to:
Create Arduino sketch (code) and program Arduino/Genuino 101 board
Understand the working of available timers in Arduino/Genuino 101
Set interrupt at variable intervals using timers
Use timer 1 to make a count-down timer switch
Turn on the on-board LED (or external relay) for a specified time
Working principle of key components
Before performing this lab experiment, it is important to learn following concepts:
Arduino/Genuino 101 is a low-power consumption and high-performance version of Arduino board with Bluetooth low-energy (BLE) and accelerometer on board. It is compatible with Arduino/Genuino Uno in term of form factor and peripheral list. (For more details, please visit: https://www.arduino.cc/en/Main/ArduinoBoard101).
Arduino/Genuino 101 can be easily programmed using Arduino IDE (version 1.6.7 and higher). However, the hardware libraries for Arduino/Genuino 101 board needs to be updated / installed. (Refer appendix for detailed procedure).
CurieTimerOne library provides access to the Timer1 of Arduino/Genuino 101. Using this library, you can set up the time (in microseconds) that the timer would count before asserting an interrupt (further details on the library is available at: https://www.arduino.cc/en/Reference/CurieTimerOne).
The interrupt can be configured to call a specific function (callback function) that will be executed every time the interrupt is asserted.
Typically, a timer interrupt is used in making of timer switch or generating fully customizable Pulse Width Modulated (PWM) signals.
A timer switch is a time-controlled switch that has user-defined on or off time. Once the timer is enabled, the timer waits till the specified time and then makes the switching.
Key commands
Before programming the Arduino/Genuino 101, it is important to learn following key commands:
Serial.parseInt
Reads from the serial port and parses the “,” separated string into integer value
CurieTimerOne.start
Starts the timer (counting) with specified time in microseconds and attaches the given function to the interrupt for callback
delay
Halts the execution by specified time in milliseconds
digitalWrite
Turn on/off a digital pin of the Arduino / Genuino board
digitalRead
Reads the status (on or off) of a digital pin
Check Your Understanding
A timer switch, after user specified time, would ___
Turn on the connected device
Turn off the connected device
Both a & b
Not change the status of the device
CurieTimerOne library used in this example allow access to ___
Timer 1
Timer 2
Timer 3
All the timers
The instruction “delay(500)” would ___
Halt the program execution for 500 seconds
Halt the program execution for 500 milliseconds
Halt the program execution for 500 microseconds
Halt the program execution for 500 nanoseconds
Which command will start the timer with given time and attach the callback function?
CurieTimerOne.attachInterrup()
CurieTimerOne.restart()
CurieTimerOne.resume()
CurieTimerOne.start()
Which of the following command can be used to read integer values from serial port?
Serial.parseInt
Serial.println
Serial.readInt
Serial.print
A timer switch could also be used for _____
Generating PWM
Keeping a device continuously off for 24 hours
Keeping a device continuously on for 24 hours
All of the above
Time value for “CurieTimerOne.start” command is specified in
seconds
milliseconds
microseconds
nanoseconds
The answers to the above questions can be found at Appendix B.
Procedure
Hardware Setup
Connect the Arduino/Genuino 101 board with computer using USB cable.
Arduino IDE / Library Setup
Make sure you have Arduino IDE version 1.6.7 or higher and Intel Genuino 101 drivers installed on your computer. For installation of Arduino IDE and drivers, you can follow instructions from Appendix A.
Creating Sketch / Program
Open the sketch (G101_Ex-5_Timer-Switch.ino) on Arduino IDE.
From Tools menu, select the right board (i.e., Arduino/Genuino 101) and COM Port it is connected to.
Compile (verify) and run (upload) the sketch on Arduino board. In case of any upload error, try pressing the Master Reset Button just at the start of upload process.
Open the “serial monitor” of Arduino IDE. It will be used to display the timer switch state, down-counting and to enter time for the switch.
The serial monitor would instruct you to specify two values separated by a comma – [a] enabling of the timer (any value > 0) and [b] time value in seconds.
Suppose you want to time the switch for 50s, then enter “1, 50” and hit “Send”.
You would see – [a] LED on board glows and [b] the count-down starts.
Once the counting to specified time is complete, the LED is turned off and user is prompted to specify the time again.
Additional Exercise
You can extend your learning by trying following programming exercises:
Extend the existing system to control another device (say a motor). The motor should run only for the specified time and when user wants to run it.
Combine the current program with Bluetooth and make a timer switch which can be enabled and time-specified using a Bluetooth app.
Last updated