Introduction: Intel® Edison Board: Clap Activated Clock
This tutorial will go over how to create a clap-on clock with the added bonus of telling the temperature and adjusting display brightness based on ambient light levels.
We used an Intel® Edison board along with a light sensor, temperature sensor, sound sensor, and LED display. When you clap your hands (or made any other loud noise), the LED display lights up and displays the time for 10 seconds and afterwards displays the temperature for 3 seconds. Depending on the ambient light level in the room, the brightness of LEDs is adjusted for visibility for day and night conditions. The program is written in C++ using the Intel® IoT Developer Kit version of the Eclipse* IDE.
For more information on the Intel® Edison board: https://www-ssl.intel.com/content/www/us/en/do-it...
To install the Eclipse* IDE and how to get started: https://software.intel.com/en-us/installing-the-e...
Components:
- Intel Edison Board
- From the Grove Kit:
- Seeed Studio* Base Shield
- Temperature Sensor
- Sound Sensor
- Light Sensor
- MAX7219 8 Digit 7 Segment LED Display (and 5 wires)
The majority of the sensors are from the Grove* Starter Kit for Intel® Edison board which is designed to make bread boarding and wiring circuits easy. The base shield slots in on top of the Intel® Edison board and with the Grove cables, you simply plug in one end to the sensor and the other end to the slot on the shield. Always make sure the board is not powered on when changing wires and cables around. Also toggle the Grove shield to be set on 5V, otherwise the sensors in this tutorial will not have enough power. You can read more about the Grove Starter Kit here: http://www.seeedstudio.com/depot/Grove-starter-ki...
Step 1: Sensors - Temperature Sensor
The temperature sensor should be plugged into slot A0 for analog signals. The temperature will be recorded in degrees Celsius.
Step 2: Sensors - Sound Sensor
The sound sensor is the main component of this demo to hear the clapping, plug it into slot A2.
Step 3: Sensors - Light Sensor
Make sure nothing is blocking the light sensor from seeing the source of light in the room and plug it into slot A1.
Step 4: Sensors - MAX7219 8 Digit 7 Segment LED Display
The LED display uses a MAX7219
chip that has a 4 wire SPI interface (it relies on the clock to synchronize data transmission) in addition to the 5V power connection wire. So connections from the LED pin to Edison board pin will be: VCC to 5V, GND to GND, and DIN (Data in) to pin 11, CS to pin 10, and CLK (clock) to pin 13. The comments in the code go into more detail on how to display different numbers and letters if you would like to customize it.
Step 5: Code
The following is C++ code for the Eclipse* IDE for Intel® Edison, to install it and how to get started: https://software.intel.com/en-us/installing-the-e...
/*
* Author: Dirk Roziers
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "mraa.h"
#include
#include
#include
int main(int argc, char** argv)
{
time_t current_time;
struct tm * time_info;
char timeString[9]; // space for "HH:MM:SS\0"
int hours, minutes, seconds;
unsigned int temp_value, light_value, sound_value;
int counter = 1;
int display_on = 0;
unsigned int sound_threshold = 400;
float max_light_value = 800.0;
int number_of_sec_to_display_time = 10;
int number_of_sec_to_display_temp = 3;
float resistance;
float temp_celcius;
int temp_celcius_int;
int temp_celcius_decimal_1;
int brightness;
mraa_aio_context temp_pin;
mraa_aio_context light_pin;
mraa_aio_context sound_pin;
/* value of each segment according to binary representation in datasheet
64
------
| |
2 | | 32
| 1 |
------
| |
4 | | 16
| |
------
8
index in array = value of digit,
ie digit_value[5] = displays 5 on LED,
digit_value[7] = displays 7 on LED,
digit_value[10] = blank LED
*/
int digit_value[] = {
126, /* 2+4+8+16+32+64 */
48, /* 16+32 */
109, /* 1+4+8+32+64 */
121, /* 1+8+16+32+64 */
51, /* 1+2+16+32 */
91, /* 1+2+8+16+64 */
95, /* 1+2+4+8+16+64 */
112, /* 16+32+64 */
127, /* 1+2+4+8+16+32+64 */
115, /* 1+2+16+32+64 */
0
};
/*
* binary value to be sent over SPI to display to display value vvvv vvvv on LED number nnnn is : 0000 nnnn vvvv vvvv
* e.g. setting 5th LED from left ( = 4th LED from right )to value 5 ( = 91, see above = 10011011 ) --> pass 0000 0100 1001 1011
* decimal = 1024 ( 0000 0100 0000 0000 ) + 91 ( 0000 0000 1001 1011 ) = 1115
*/
int digit_position[] = {
256, /* digit_position[0] = 8th digit = rightmost digit, 256 = 0100 hex = 0000 0001 0000 0000 binary */
512, /* digit_position[1] = 7th digit = 2nd from right, 512 = 0200 hex = 0000 0010 0000 0000 binary */
768, /* digit_position[2] = 6th digit */
1024, /* digit_position[3] = 5th digit */
1280, /* digit_position[4] = 4th digit */
1536, /* digit_position[5] = 3rd digit */
1792, /* digit_position[6] = 2nd digit */
2048 /* digit_position[7] = 1st digit = leftmost digit */
/* index in array = 8 - position of digit on display ; left most digit = position 1 */
};
// temp sensor on analog Pin0 ( Aio0 ), light sensor on analog Pin1 ( Aio1 ) and sound sensor on analog Pin2 ( Aio2 )
temp_pin = mraa_aio_init(0);
light_pin = mraa_aio_init(1);
sound_pin = mraa_aio_init(2);
mraa_spi_context spi;
spi = mraa_spi_init(1);
if (spi == NULL)
{
printf("Initialization of spi failed, check syslog for details, exit...\n");
exit(1);
}
// Set click frequency to 400kHz )
mraa_spi_frequency(spi, 400000);
mraa_spi_lsbmode(spi, 0);
// The MAX7219/21 Chip needs the data in word size
if (mraa_spi_bit_per_word(spi, 16) != MRAA_SUCCESS)
{
printf("Could not set SPI Device to 16Bit mode, exit...\n");
exit(1);
};
mraa_spi_write_word(spi, 0x0900); // Do not decode bits
mraa_spi_write_word(spi, 0x0a0f); // Brightness of LEDs -- 0xa00 lowest, 0xa0f highest -- set to highest to start with
mraa_spi_write_word(spi, 0x0b07); // Show all Scan Lines
mraa_spi_write_word(spi, 0x0c00); // Display off -- start with display off.
mraa_spi_write_word(spi, 0x0f00); // Testmode off
while(true){
while ( display_on == 0 ){
//detect sound to switch display on
sound_value = mraa_aio_read(sound_pin);
if ( sound_value > sound_threshold ){
display_on = 1;
mraa_spi_write_word(spi, 0x0c01); // Display on
}
usleep(1000);
}
while ( display_on == 1 )
{
temp_value = mraa_aio_read(temp_pin);
light_value = mraa_aio_read(light_pin);
// Convert temp_value reading into Celcius using datashaeet - Bvalue of sensor = 3975
resistance = (1023 - temp_value) * 10000 / temp_value; // Get the resistance of the sensor
temp_celcius = (1 / (log(resistance / 10000) / 3975 + 1 / 298.15) - 273.15); // Convert to temperature via datasheet
// separate integer and decimal for easier displaying purposes
temp_celcius_int = temp_celcius;
temp_celcius_decimal_1 = temp_celcius*10 - temp_celcius_int*10;
time(¤t_time);
time_info = localtime(¤t_time);
strftime(timeString, sizeof(timeString), "%H:%M:%S", time_info);
hours = time_info->tm_hour;
minutes = time_info->tm_min;
seconds = time_info->tm_sec;
/* TEMP DISPLAY LAYOUT xxTT.txxx */
uint16_t TEMPdata[] = {
/* 8th digit */ digit_position[0]+digit_value[10],
/* 7th digit */ digit_position[1]+78, // 78 = value of character C (Clecius)
/* 6th digit */ digit_position[2]+digit_value[10],
/* 5th digit */ digit_position[3]+digit_value[temp_celcius_decimal_1],
/* 4th digit */ digit_position[4]+digit_value[temp_celcius_int%10]+128,
/* 3rd digit */ digit_position[5]+digit_value[(temp_celcius_int-temp_celcius_int%10)/10],
/* 2nd digit */ digit_position[6]+digit_value[10],
/* 1st digit */ digit_position[7]+digit_value[10]};
/* TIME DISPKAY LAYOUT 1 xxHH.MMxx */
uint16_t TIMEdata[] = {
/* 8th digit */ digit_position[0]+digit_value[10],
/* 7th digit */ digit_position[1]+digit_value[10],
/* 6th digit */ digit_position[2]+digit_value[minutes%10]+(seconds%2)*128,
/* 5th digit */ digit_position[3]+digit_value[(minutes - minutes%10)/10],
/* 4rd digit */ digit_position[4]+digit_value[hours%10]+128,
/* 3nd digit */ digit_position[5]+digit_value[(hours - hours%10)/10],
/* 2nd digit */ digit_position[6]+digit_value[10],
/* 1st digit */ digit_position[7]+digit_value[10]};
/* TIME DISPKAY LAYOUT 2 HH.MM.SS *
/ uint16_t TIMEdata[] = {
// /* 8th digit */ digit_position[0]+digit_value[10],
// /* 7th digit */ digit_position[1]+digit_value[seconds%10],
// /* 6th digit */ digit_position[2]+digit_value[(seconds - seconds%10)/10],
// /* 5th digit */ digit_position[3]+digit_value[minutes%10]+128,
// /* 4th digit */ digit_position[4]+digit_value[(minutes - minutes%10)/10],
// /* 3rd digit */ digit_position[5]+digit_value[hours%10]+128,
// /* 2nd digit */ digit_position[6]+digit_value[(hours - hours%10)/10],
// /* 1st digit */ digit_position[7]+digit_value[10]};
/* TIME DISPKAY LAYOUT 3 HH MM SS *
/ uint16_t TIMEdata[] = {
// /* 8th digit */ digit_position[0]+digit_value[seconds%10],
// /* 7th digit */ digit_position[1]+digit_value[(seconds - seconds%10)/10],
// /* 6th digit */ digit_position[2]+digit_value[10],
// /* 5th digit */ digit_position[3]+digit_value[minutes%10],
// /* 4th digit */ digit_position[4]+digit_value[(minutes - minutes%10)/10],
// /* 3rd digit */ digit_position[5]+digit_value[10],
// /* 2nd digit */ digit_position[6]+digit_value[hours%10],
// /* 1st digit */ digit_position[7]+digit_value[(hours - hours%10)/10]};
brightness = 2560+(light_value/max_light_value)*15; // brightness level 0 to 15, command 0x0a00 --> 0x0a0f // 2560 --> 2575
mraa_spi_write_word(spi, brightness); // Brightness of LEDs
if ( counter == number_of_sec_to_display_time+1 )
{
mraa_spi_write_buf_word(spi, TEMPdata, 2);
mraa_spi_write_buf_word(spi, TEMPdata, 4);
mraa_spi_write_buf_word(spi, TEMPdata, 6);
mraa_spi_write_buf_word(spi, TEMPdata, 8);
mraa_spi_write_buf_word(spi, TEMPdata, 10);
mraa_spi_write_buf_word(spi, TEMPdata, 12);
mraa_spi_write_buf_word(spi, TEMPdata, 14);
mraa_spi_write_buf_word(spi, TEMPdata, 16);
usleep(number_of_sec_to_display_temp*1000000); /*display temp for number_of_sec_to_display_temp seconds*/
counter=1;
display_on = 0;
mraa_spi_write_word(spi, 0x0c00); // Display off
}
else
{
mraa_spi_write_buf_word(spi, TIMEdata, 2);
mraa_spi_write_buf_word(spi, TIMEdata, 4);
mraa_spi_write_buf_word(spi, TIMEdata, 6);
mraa_spi_write_buf_word(spi, TIMEdata, 8);
mraa_spi_write_buf_word(spi, TIMEdata, 10);
mraa_spi_write_buf_word(spi, TIMEdata, 12);
mraa_spi_write_buf_word(spi, TIMEdata, 14);
mraa_spi_write_buf_word(spi, TIMEdata, 16);
usleep(1000000);
counter++;
}
}
}
}
Step 6: End
About the Content Creator
Dirk Roziers is an IoT Developer Community Evangelist in the Software and Services Group, working on building out and supporting an Intel IoT developer community in EMEA.
About the Author
Whitney Foster is a software engineer at Intel in the Software Solutions Group working on scale enabling projects for Android applications and IOT.
Notices
No license (express or implied, by estoppel or otherwise) to any intellectual property rights is granted by this document.
Intel disclaims all express and implied warranties, including without limitation, the implied warranties of merchantability, fitness for a particular purpose, and non-infringement, as well as any warranty arising from course of performance, course of dealing, or usage in trade.
This document contains information on products, services and/or processes in development. All information provided here is subject to change without notice. Contact your Intel representative to obtain the latest forecast, schedule, specifications and roadmaps.
The products and services described may contain defects or errors known as errata which may cause deviations from published specifications. Current characterized errata are available on request.
Copies of documents which have an order number and are referenced in this document may be obtained by calling 1-800-548-4725 or by visiting www.intel.com/design/literature.htm.
Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.
*Other names and brands may be claimed as the property of others
**This sample source code is released under the Intel Sample Source Code License AgreementLike SubscribeAdd new commentFlag as spam .
© 2015 Intel Corporation.Flag as inappropriate Flag as Outdated