Posts with «arduinobasics» label

Maker Display to Ubidots MQTT button


See the original project on the ArduinoBasics Blog

 
 

Description

This tutorial will show you how to create a simple MQTT connection to Ubidots. You will also learn to configure the necessary MQTT subscription to a button on the Ubidots dashboard, and control a Maker Display (ESP-12E compitible board) from anywhere in the world. The process may seem a bit daunting at first, but hopefully by the end of this tutorial, you will feel comfortable creating your own Ubidots MQTT subscriptions.

 
 

Parts Required

  1. Maker Display 2 or Node MCU (ESP-12E) module
  2. NOVA programmer
  3. USB mini-B cable
  4. WiFi internet connection
  5. Ubidots account (free)

The Maker display 2 has an inbuilt ESP8266MCD WiFi module which will be used to create the MQTT connection to the Ubidots broker (online).

 
 

Ubidots Setup

This tutorial requires a FREE Ubidots account.
Go to this site to sign up: https://ubidots.com/education/
Once signed up, you will need to configure Ubidots using the following instructions.

Create a device

  1. Select: Devices > Devices
  2. Select: Create a Device (button)
  3. Select: Blank (from the available device list)
  4. Enter the "Maker Display" into the "Device name" field, "maker-display" into the "Device label" field, and click on "Create" button
  5. Select: the "Maker Display" device

Create a variable

  1. Select: "Add Variable" button, then select "Raw" from the two available options.
  2. Select: the "New Variable" to edit it
  3. Change the name to "Button 1", the description to "button1 variable" and the API label to "button1"

Create a dashboard

  1. Select: Data > Dashboard
  2. Select: Add new dashboard
  3. Change the Name to "Maker Display Dashboard", and update the date format to a suitable format. (press tick)

Add a Widget

  1. Select: "Add new Widget"
  2. Select: Switch (from the available widgets)
  3. Select: Add Variables
  4. Select: Maker Display > Button1 > tick
  5. Accept the default values for the Switch (Off=0, On=1), and press the tick
  6. You should now have a button called "Button1" associated with the "Maker Display" device, visible on the "Maker Display Dashboard"
  7. The button is "off".

Create a Ubidots TOKEN

  1. Select: "API Credentials" from the profile drop-down box in the top right corner.
  2. Click the blue "More" - located below the Tokens section
  3. Click on the round blue (+) button to create a NEW TOKEN.
  4. Change the name to "Maker Display Token" - and keep a record of TOKEN value. There is an icon which will allow you copy the TOKEN value to the clipboard.

Take note of key information

Now that the Ubidots Dashboard is set up, you will need to ensure you have 3 sets of information to insert into the code.

  1. Maker Display Token Value
  2. Button1 API label: "button1"
  3. Maker Display Device API Label: "maker-display"

 
 

Ubidots slideshow of the setup process

Slide Set created by Scott C with GoConqr

 
 

Arduino IDE

While there are many Arduino IDE alternatives out there, I would recommend that you use the official Arduino IDE for this project. I used the official Arduino IDE app (v1.8.5) for Windows 10.
Make sure to get the most up-to-date version for your operating system here.

Additional Boards Manager URLS

Make sure to add the following URLs to your "additional boards manager URL" setting:
  • File > Preferences > Additional Boards Manager URLS:
    • http://arduino.esp8266.com/stable/package_esp8266com_index.json
    • https://dl.espressif.com/dl/package_esp32_index.json

Select Tools > Board: "NodeMCU 1.0 (ESP-12E Module)" board.
Then check that you have the following settings:

  • Board:"NodeMCU 1.0 (ESP-12E Module)"
  • Flash Size:"4M(no SPIFFS)"
  • Debug port:"Disabled"
  • Debug Level:"None"
  • IwIP Variant:"v2 Lower Memory"
  • VTables:"Flash"
  • CPU Frequency:"80 MHz"
  • Exceptions:"Disabled"
  • Upload Speed:"115200"
  • Erase Flash:"Only Sketch"
  • Port: (Select your port)
  • Get Board Info
  • Programmer:"AVRISP mkII"


 
 

Libraries required

This tutorial makes use of two libraries: ESP8266WiFi.h and PubSubClient.h.

  • ESP8266WiFi.h : This library is required for the WiFi connection to the internet. More info.
  • PubSubClient.h : Is used to create an MQTT Client to handle the communication between the Ubidots MQTT broker and the Maker Display2 (or ESP-12E).

Both libraries can be installed via the library manager: Sketch > Include library > Manage Libraries



 
 
 
 

Arduino Code

Connect the NOVA programmer to the Maker Display 2

Remember that you will need to insert the 3 bits of information from the "Ubidots" section, into the code. You will also need to know your WiFI SSID name, and password. Copy the code below into the Arduino IDE, make the necessary changes in the sketch to reflect the API labels and tokens from your Ubidots account. Connect the USB cable to the computer, select the correct COM port (Tools > Port), then upload the code to the Maker display board.

The code is available on my GitHub repository. Or you can have a look at the fully commented code below.

 
 

Open the Serial monitor (ctrl + shift + M), ensure the Baud is set to 9600, and then press the button on the Ubidots Maker Display Dashboard. You should see messages appear in the Serial monitor that correspond with the state of the button.

 
 

Code Explained

A number of different information sources were utilised to construct the code above. These sources were acknowledged within. As noted before, the code uses two libraries, one to simplify the WiFi connection to the internet, and the other to simplify the connection of the Maker Display to the Ubidots MQTT broker.

setup()

The setup() function is used to establish the WiFi connection, set the MQTT broker, and define the callback function - which will be called each time the button on the Ubidots dashboard is pressed.

loop()

The loop() function is responsible for connecting to the MQTT broker, and polling for messages from the MQTT broker using the client.loop() function.

callback()

The callback() function first checks the "topic" message coming from the MQTT broker, and compares it to the buttonTopic variable. The buttonTopic variable in this sketch is equal to "/v1.6/devices/maker-display/button1/lv". You will notice that the variable is constructed using the "device API label", and the "button API label". The other components of the buttonTopic are always the same. eg.

"/v1.6/devices/{device label}/{variable label}/lv"

This comparison allows us to differentiate this particular button from other potential components on the Ubidots dashboard. The value of the button (on=1/off=0), is transmitted from the MQTT broker each time the button is pressed on the Ubidots Maker Display dashboard. It is captured by the "payload" variable in the callback function. If the payload variable is equal to 1 (on), then a Serial message will be transmitted "BUTTON ON". If the payload variable is equal to 0 (off), then a Serial message will be transmitted "BUTTON OFF". You will be able to see this message come through by opening the Serial Monitor (ctrl+shift+M) within the Arduino IDE.

MQTTconnect()

The MQTTconnect() function is responsible for connecting to the MQTT broker. It requires a Ubidots TOKEN, a unique MQTT client name and a pre-defined port (1883). In order to receive messages from the button on the Ubidots Maker Display dashboard, we need to subscribe to button. But first we need to construct the string location of the button variable.  
 
We do this using the sprintf() function.  
 
If you would like to learn more about the sprintf function have a look at my tutorial here.  
 
The sprintf function is used to construct the string in a specific format, and in this case it uses the DEVICE_LABEL, and VARIABLE_LABEL1, and assigns the string to the buttonTopic variable. Once constructed the buttonTopic variable is used to subscribe to the button on the Ubidots dashboard.

The program is instructed to retry the connection attempt every 2 seconds if it fails to connect for any reason.

 
 

Project in Action


 
 

Conclusion

Setting up the Ubidots dashboard and the Arduino IDE takes up the majority of the time in this project. Once all the configurations are made, the rest is pretty simple. I hope by the end of this tutorial, you will have learnt how to create a button on a Ubidots dashboard, and interface it with a Maker Display (ESP-12 compatible board).

While there are plenty of examples on the Ubidots website that will show you how to push data to the dashboard, I found that the opposite was not true. There are limited examples that show you how to control your device from a Ubidots widget. I know that this example is not that exciting, but hopefully, you understand the significance of the information, and understand how easy it would be to modify the sketch above to make it more exciting. However, I did not want to over-complicate the tutorial with other complexities. Perhaps in the next tutorial we will use the information gained here, to do something a bit more thrilling. But at least now you know the basics. We can now:

  • Program a Maker Display
  • Add widgets to a Ubidots dashboard
  • Control the Maker Display using widgets from a Ubidots dashboard (using MQTT)

If you found this tutorial helpful, please consider supporting me by buying me a virtual coffee/beer.

$3.00 AUD only
 

Social Media

You can find me on various social networks:

Follow me on Twitter: ScottC @ArduinoBasics.
I can also be found on Instagram, Pinterest, and YouTube.
And if all else fails, I have a server on Discord.



             

sprintf function

Description

This tutorial will help you to understand the sprintf function, and how to use it. Essentially, the sprintf function allows you to construct a string using a pre-formatted string template to which you can insert variables at pre-defined locations. The sprintf function will "compile" the string and assign it to a char array. All you have to do is make sure that the char array is large enough to hold all of the characters in the string. The best way to understand the sprintf function is with examples. And luckily, I have examples. What are we waiting for ? Let's dive in.

Parts Required: an Arduino and a USB cable.

 
 

sprintf ( char* array,   const char* strTemplate,   var1...);

 
 

Arduino IDE

While there are many Arduino IDE alternatives out there, I would recommend that you use the official Arduino IDE for this project. I used the official Arduino IDE app (v1.8.5) for Windows 10.
Make sure to get the most up-to-date version for your operating system here.


 
 

Arduino Code

The code below will show you how to use the sprintf function and includes a number of different format specifiers to play with. In each case the sprintf function writes to the "data" character array, and subsequently sends it through to the Serial monitor. The string template helps to construct the data output, allowing you to insert variables at specific locations within the text. The format of the variable is defined by the "format specifier" used in the sprintf function. The format specifier is always prefixed with a percentage sign (%).

 
 

Serial Monitor Output

  1. Upload the code to the Arduino.
  2. Open the Serial monitor in the Arduino IDE (Ctrl+Shift+M).
  3. Ensure that you have set the baud rate in the Serial monitor to 9600.
  4. You should see the following output:

 

The sprintf function requires that you have a character array to store the output. In the example code above, the output is stored in the "data" character array. It also requires a template that tells the function where to insert the variables. As you can see from the table below, the variables will be formatted based on the format specifier used. The format specifier can be quite useful for numeric conversions. Eg. decimal to hex conversions.

 
 

Format Specifiers

Some of the different format specifiers that can be used with the sprinf function are listed below.

Conclusion

Now that you know all about the sprintf function, I hope it will inspire you to use it in your own projects. Please let me know in the comments below how you use the sprintf function, and whether there was anything that you feel I failed to mention in this tutorial.
Happy Coding !!

 
 

If you found this tutorial helpful, please consider supporting me by buying me a virtual coffee/beer.

$3.00 AUD only
 

Social Media

You can find me on various social networks:

Follow me on Twitter: ScottC @ArduinoBasics.
I can also be found on Instagram, Pinterest, and YouTube.
And if all else fails, I have a server on Discord.


Arduino based Security Project Using Cayenne


 

Description

This is an Arduino based home security project that uses the power of "Cayenne" for extraordinary capabilities.

Cayenne Beta

Cayenne is a new IoT drag and drop platform originally released for the Raspberry Pi, but now available for Arduino. Cayenne makes the task of connecting your Arduino to the internet as simple as possible. All of the complexity of internet connectivity is hidden within the Cayenne library.

You can easily create a Network of Arduinos and build an IoT system which can be managed and operated within the Cayenne dashboard. This dashboard is accessible through your browser or via the Cayenne smart phone app (on IOS or Android).

The feature I liked the most, was the ability to change the position of sensors or actuators on the Arduino without having to re-upload Arduino code. I could manage the changed position from within the Cayenne platform. The other feature that I liked was the ability to setup actions based on custom triggers. You can use Cayenne to trigger a whole range of functions, for example: play a sound, move a motor, light up an LED, or to send alert notifications via email or SMS.

Cayenne is in Beta at the moment, so there are a few minor bugs here and there, but overall - I give it a thumbs up - it is definitely worth checking out.
 

Here is a link to the Cayenne Beta Program:
**Cayenne Beta Link**



              Source: myDevices Media Kit

 

Home Security Project Summary

In order to fully experience this new IoT platform, I decided to create a project to really put it through its paces. This is what my Security Project will need:

  1. It will use two Arduinos, one connected to the internet via an Ethernet shield, and the other via WIFI.
  2. Two detectors - a PIR sensor and a laser trip wire.
  3. If the sensors are tripped, the person has 10 seconds to present an RFID tag to the Grove RFID reader:
    • If a valid RFID tag is SUCCESSFULLY presented within the time limit, a nice personalised greeting will be played to that person using a Grove - Serial MP3 player
    • If a valid RFID FAILS to be presented within the time limit, an Alarm will sound, and I will be notified of the intrusion via an SMS alert.
  4. The Cayenne dashboard will show the status of the sensors, and I will have full control over my security system via the web interface (or smartphone app).
  5. The sensors will be attached to a different Arduino to that of the Grove MP3 player and the RFID tag reader, which means that there will have to be some level of communication between the two Arduinos. In fact, the cross communication will be vital to the success of this project.


 
 
 

Project Video



 
 
 
 

 

Flow Diagrams:

Main Flow Diagram

The following flow diagram shows the Security project process. It is a high level view of the decisions being made by each Arduino in response to various events.  


 

Triggers Flow Diagram

The following flow diagram aims to highlight the various triggers set up within Cayenne to get this Security system to work.  

 
 
 

Arduino IDE and Library Downloads

You will need an Arduino IDE to upload code to the Arduino and the Seeeduino Cloud.
Here is the link to the Arduino IDE: Arduino IDE - download location

The Cayenne service requires that you download and install the Cayenne Library into your Arduino IDE.
You can get the Cayenne Library from here: Cayenne Library File - Download


 

Cayenne Connectivity Setup

The Seeeduino Cloud needs to be prepared for use with Cayenne.
Normal operating/setup instructions can be found here: Seeeduino Cloud WIKI page
 
Once you have successfully connected Seeeduino Cloud to your WIFI network, you can add it to the Cayenne Dashboard by making the following selections from within the Cayenne Web application:

  1. Add New
  2. Device/Widget
  3. Microcontrollers
  4. Arduino
  5. Ensure Seeeduino Cloud is connected to WIFI network - the select the NEXT button
  6. Select - Arduino Yun: Built-in Ethernet - ticked
  7. Providing you have already installed the Cayenne library as described above - you should be able to copy and paste the code to the Arduino IDE and upload to the Seeeduino Cloud.
  8. If successful, you should see the Arduino Yun board appear within the Cayenne Dashboard. If not, then seek help within the Cayenne forum.


 

The Arduino UNO with WIZNET 5100 - Ethernet Shield
also needs to be prepared with Cayenne

  1. Add New
  2. Device/Widget
  3. Microcontrollers
  4. Arduino
  5. Ensure Arduino is powered, and Ethernet shield is connected to your internet router via an Ethernet cable
  6. Select - Arduino Uno: Ethernet Shield W5100 - ticked
  7. Copy and paste the code to the Arduino IDE and upload to the Arduino UNO.
  8. If successful, you should see the Arduino Uno board appear within the Cayenne Dashboard. If not, then seek help within the Cayenne forum.

 


 
If you have the Ethernet shield with the WIZNET 5200 chip, then you may need to download a specific Ethernet library in addition to the Cayenne library.
 
Just follow the instructions within the Automatically generated sketch provided - when you select your specific Arduino/Ethernet/WIFI shield combination. If you need further instructions on connecting your device to Cayenne - then please visit the myDevices website for the online documentation.
 


 
 
 
 

ARDUINO CODE (1)


Code for Arduino UNO with Ethernet Shield:

The following code will need to be uploaded to the Arduino UNO:


 
 
 
 
 

ARDUINO CODE (2)


Code for Seeeduino Cloud:

The following code will need to be uploaded to the Seeeduino Cloud:


 
 
 
 

Fritzing diagram (1)


Fritzing diagram for Arduino UNO with Ethernet

Please click on the picture below for an enlarged version of this fritzing diagram


 
 
 
 

Fritzing diagram (2)


Fritzing diagram for Seeeduino Cloud

Please click on the picture below for an enlarged version of this fritzing diagram


 
 
 
 
 
 
 

Cayenne Dashboard Setup - GUI


The Arduino code only provides half of the functionality of this project. The Cayenne Dashboard needs to be setup to provide the rest of the functionality. The following instructions will show you how to add each of the widgets required for this Home Security project.


Arduino Ethernet - Master Switch

The master switch allows me to turn the security system on and off. When I turn the MASTER SWITCH ON, the laser beam will turn on, and the sensors will start monitoring the area for intruders. This widget is NOT associated with a physical switch/sensor on the Arduino - it uses virtual channel 0. We need to add the Master switch to the dashboard:


  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. Digital Output - Control a Digital Output
  6. Widget Name: Master On Off Switch
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V0
  10. Choose Widget: Button
  11. Choose Icon: Valve
  12. Step2: Add Actuator
We will add a trigger later to get this button to automatically turn the Laser beam on.


 
 
 

Arduino Ethernet - PIR Sensor

This sensor will be used to detect movement in the room. If a person walks into the room, this sensor will detect movement, and will trigger a message to be played on the Grove Serial MP3 player. The message will aim to get the person to identify themselves. They identify themselves by placing their RFID tag in close proximity to the Grove RFID reader. If the tag is valid, a "Welcome home" message is played on the Grove MP3 player. If a valid tag is not presented to the reader within 10 seconds, an Alarm will go off ("Alarm sound" played on Grove MP3 player.)

The PIR sensor is connected to digital Pin 6 of the Arduino, however, it is mapped to virtual pin 1 for better synchronisation with the Cayenne dashboard. This was done to capture ALL detections - as the PIR sensor could change from a LOW to HIGH and back to LOW state in between a Cayenne state check - and therefore, Cayenne could miss this motion detection.. Therefore we need to assign the PIR sensor to a virtual channel in the following way:
  1. Add New
  2. Device/Widget
  3. Sensors
  4. Motion
  5. Digital Motion Sensor - Motion Detector
  6. Widget Name: PIR sensor
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V1
  10. Choose Widget: 2-State Display
  11. Choose Icon: Light
  12. Step2: Add Sensor
  13. Select Settings from the PhotoResistor
  14. Choose Display: Value
  15. Save

 
 
 

Arduino Ethernet - Photoresistor

This sensor will be used with the laser beam to create a laser tripwire. If the sensor detects a change in light levels (drops below the threshold), it will activate the laser trigger button on the dashboard. The person will then be required to identify themselves etc etc (similar to the motion detection by the PIR sensor). The photoresistor widget will display the raw analog reading from the sensor (connected to A2), but is associated with virtual channel 2. I used a virtual channel for more control over this sensor. To add the Photoresistor to the dashboard:

  1. Add New
  2. Device/Widget
  3. Sensors
  4. Luminosity
  5. Photoresistor - Luminosity sensor
  6. Widget Name: PhotoResistor
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V2
  10. Choose Widget: Value
  11. Choose Icon: Light
  12. Step2: Add Sensor


 
 
 

Arduino Ethernet - Laser Trigger

The laser trigger is just an indicator that someone tripped the laser beam. The state of this widget is used to notify the Seeeduino that a presence has been detected. This widget is associated with virtual pin 4 on the Arduino UNO with Ethernet.

  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. Digital Output - Control a Digital Output
  6. Widget Name: Laser Trigger
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V4
  10. Choose Widget: Button
  11. Choose Icon: Lock
  12. Step2: Add Actuator


 
 
 

Arduino Ethernet - Laser Threshold

The laser threshold is used to manually configure the light level at which the laser trigger will trip. When the photoresistor value drops below the threshold value, the laser trigger icon will activate. This allows the threshold value to be updated from the Cayenne dashboard, rather than having to manually adjust the value in the Arduino code. Also, this threshold can be set remotely, in that you don't have to be near the Arduino to change this value. A very useful feature of this Security system. This widget is associated with virtual pin 5 on the Arduino UNO with Ethernet.

  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. PWM Output - Control a PWM Output
  6. Widget Name: Laser Threshold
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V5
  10. Choose Widget: Slider
  11. Slider Min Value: 0
  12. Slider Max Value: 10
  13. Step2: Add Actuator
The max value of the slider is 10 - due to a current bug in the Cayenne software. Once resolved, this value (as well as the relevant Arduino code) will need to be updated.


 
 
 

Seeeduino Cloud - Presence Detected

The presence detected widget is there to notify the Seeeduino Cloud that a presence has been detected on the Arduino Uno with Ethernet shield. When the PIR sensor detects movement or if the laser tripwire is tripped, Cayenne will change the state of the Presence Detected widget from LOW to HIGH. This is used within the Seeeduino Cloud to trigger the message "Place your keys on the Mat"
. If a valid RFID tag is read by the Grove RFID reader, then this widget's state will change back from HIGH to LOW, and the MasterSwitch will be deactivated - turning the Security system off. This widget is associated with Virtual pin 6 on the Seeeduino Cloud.

  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. Digital Output - Control a Digital Output
  6. Widget Name: Presence Detected
  7. Select Device: Seeeduino Cloud
  8. Connectivity: Virtual
  9. Pin: V6
  10. Choose Widget: Button
  11. Choose Icon: Lock
  12. Step2: Add Actuator


 
 
 

Seeeduino Cloud - Intruder Alert

If a valid RFID tag is not read by the Grove RFID reader within 10 seconds of a presence detection event, an alarm will sound, and this widget will be activated. This will trigger a notification event - to notify me of the unauthorised intrusion - via SMS or email. I will also have a visual indicator on the Cayenne dashboard that an intrusion has taken place. This widget is associated with Virtual pin 7 on the Seeeduino Cloud.

  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. Digital Output - Control a Digital Output
  6. Widget Name: Laser Trigger
  7. Select Device: Seeeduino Cloud
  8. Connectivity: Virtual
  9. Pin: V7
  10. Choose Widget: Button
  11. Choose Icon: Thermometer
  12. Step2: Add Actuator


 
 
 

Seeeduino Cloud - Laser Beam

The laser beam widget was created to allow for full control over the laser beam. The laser beam can be turned on or off from the Cayenne dashboard, and a connected to digital pin 7 on the Seeeduino Cloud.


  1. Add New
  2. Device/Widget
  3. Actuators
  4. Light
  5. Light Switch - Turn On/Off a Light
  6. Widget Name: xLaser Beam
  7. Select Device: Seeeduino Cloud
  8. Connectivity: Digital
  9. Pin: D7
  10. Choose Widget: Button
  11. Choose Icon: Light
  12. Step2: Add Actuator


 
 
 
 

Cayenne Triggers

Now that all of the widgets have been added to the Dashboard, there is just one more step to complete the Security System. We need to setup the triggers. These triggers provide a level of automation that is easy to create within Cayenne, but would be very complicated otherwise. I set my triggers up as per the table below. Each row represents one of the triggers within my Cayenne dashboard. If you would like to see an example of how to add a trigger - please have a look at the video at the top of this tutorial.  


 
 
 
 
 
 

Concluding comments

I used many different elements to put this home/office security project together - Multiple Arduinos were connected to the internet, both controlled by a web/smart phone app, cross-communication/synchronisation between the Arduinos, and the use of multiple sensors and modules including a laser beam !
 
This was way more than just a simple PIR sense and alarm project. I now have a personalised greeting and reminder system when I walk in the door. Everyone else has their own personalised greeting. I can enable my Security System remotely, from two blocks away, and if I wanted to - I could enable it from the other side of the world. I know instantly when someone has entered my house/office.... with an SMS alert straight to my phone.
 
This project could easily be extended:

  1. Press a button on my phone to manually trigger/play a specific message/sound/song
  2. Take a picture of the intruder
  3. Introduce fire or leak detection aswell
  4. Add other environmental sensors - Temperature / Humidity
  5. Connect it to lamp/light - creating a security light
I am sure you can think of more things I could do with this system. In fact, why don't you mention your ideas in the comments below.
 
Cayenne was instrumental in getting this project to work. I don't think I would know where to start if I had to do this project without this cool IoT platform. I think I will definitely be trying out a few more projects using Cayenne, and should you want to do the same, then please make sure to join Cayenne Beta:
 
Here is the link you need to get to the right place: Cayenne Beta Link

 

If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.

             

ScottC 02 Sep 05:11
alarm  arduino  arduinobasics  cayenne  laser  mp3  mydevices  pir  rfid  security  sms  tutorial  

Arduino based Security Project Using Cayenne


 

Description

This is an Arduino based home security project that uses the power of "Cayenne" for extraordinary capabilities.

Cayenne Beta

Cayenne is a new IoT drag and drop platform originally released for the Raspberry Pi, but now available for Arduino. Cayenne makes the task of connecting your Arduino to the internet as simple as possible. All of the complexity of internet connectivity is hidden within the Cayenne library.

You can easily create a Network of Arduinos and build an IoT system which can be managed and operated within the Cayenne dashboard. This dashboard is accessible through your browser or via the Cayenne smart phone app (on IOS or Android).

The feature I liked the most, was the ability to change the position of sensors or actuators on the Arduino without having to re-upload Arduino code. I could manage the changed position from within the Cayenne platform. The other feature that I liked was the ability to setup actions based on custom triggers. You can use Cayenne to trigger a whole range of functions, for example: play a sound, move a motor, light up an LED, or to send alert notifications via email or SMS.

Cayenne is in Beta at the moment, so there are a few minor bugs here and there, but overall - I give it a thumbs up - it is definitely worth checking out.
 

Here is a link to the Cayenne Beta Program:
**Cayenne Beta Link**



              Source: myDevices Media Kit

 

Home Security Project Summary

In order to fully experience this new IoT platform, I decided to create a project to really put it through its paces. This is what my Security Project will need:

  1. It will use two Arduinos, one connected to the internet via an Ethernet shield, and the other via WIFI.
  2. Two detectors - a PIR sensor and a laser trip wire.
  3. If the sensors are tripped, the person has 10 seconds to present an RFID tag to the Grove RFID reader:
    • If a valid RFID tag is SUCCESSFULLY presented within the time limit, a nice personalised greeting will be played to that person using a Grove - Serial MP3 player
    • If a valid RFID FAILS to be presented within the time limit, an Alarm will sound, and I will be notified of the intrusion via an SMS alert.
  4. The Cayenne dashboard will show the status of the sensors, and I will have full control over my security system via the web interface (or smartphone app).
  5. The sensors will be attached to a different Arduino to that of the Grove MP3 player and the RFID tag reader, which means that there will have to be some level of communication between the two Arduinos. In fact, the cross communication will be vital to the success of this project.


 
 
 

Project Video



 
 
 
 

 

Flow Diagrams:

Main Flow Diagram

The following flow diagram shows the Security project process. It is a high level view of the decisions being made by each Arduino in response to various events.  


 

Triggers Flow Diagram

The following flow diagram aims to highlight the various triggers set up within Cayenne to get this Security system to work.  

 
 
 

Arduino IDE and Library Downloads

You will need an Arduino IDE to upload code to the Arduino and the Seeeduino Cloud.
Here is the link to the Arduino IDE: Arduino IDE - download location

The Cayenne service requires that you download and install the Cayenne Library into your Arduino IDE.
You can get the Cayenne Library from here: Cayenne Library File - Download


 

Cayenne Connectivity Setup

The Seeeduino Cloud needs to be prepared for use with Cayenne.
Normal operating/setup instructions can be found here: Seeeduino Cloud WIKI page
 
Once you have successfully connected Seeeduino Cloud to your WIFI network, you can add it to the Cayenne Dashboard by making the following selections from within the Cayenne Web application:

  1. Add New
  2. Device/Widget
  3. Microcontrollers
  4. Arduino
  5. Ensure Seeeduino Cloud is connected to WIFI network - the select the NEXT button
  6. Select - Arduino Yun: Built-in Ethernet - ticked
  7. Providing you have already installed the Cayenne library as described above - you should be able to copy and paste the code to the Arduino IDE and upload to the Seeeduino Cloud.
  8. If successful, you should see the Arduino Yun board appear within the Cayenne Dashboard. If not, then seek help within the Cayenne forum.


 

The Arduino UNO with WIZNET 5100 - Ethernet Shield
also needs to be prepared with Cayenne

  1. Add New
  2. Device/Widget
  3. Microcontrollers
  4. Arduino
  5. Ensure Arduino is powered, and Ethernet shield is connected to your internet router via an Ethernet cable
  6. Select - Arduino Uno: Ethernet Shield W5100 - ticked
  7. Copy and paste the code to the Arduino IDE and upload to the Arduino UNO.
  8. If successful, you should see the Arduino Uno board appear within the Cayenne Dashboard. If not, then seek help within the Cayenne forum.

 


 
If you have the Ethernet shield with the WIZNET 5200 chip, then you may need to download a specific Ethernet library in addition to the Cayenne library.
 
Just follow the instructions within the Automatically generated sketch provided - when you select your specific Arduino/Ethernet/WIFI shield combination. If you need further instructions on connecting your device to Cayenne - then please visit the myDevices website for the online documentation.
 


 
 
 
 

ARDUINO CODE (1)


Code for Arduino UNO with Ethernet Shield:

The following code will need to be uploaded to the Arduino UNO:


 
 
 
 
 

ARDUINO CODE (2)


Code for Seeeduino Cloud:

The following code will need to be uploaded to the Seeeduino Cloud:


 
 
 
 

Fritzing diagram (1)


Fritzing diagram for Arduino UNO with Ethernet

Please click on the picture below for an enlarged version of this fritzing diagram


 
 
 
 

Fritzing diagram (2)


Fritzing diagram for Seeeduino Cloud

Please click on the picture below for an enlarged version of this fritzing diagram


 
 
 
 
 
 
 

Cayenne Dashboard Setup - GUI


The Arduino code only provides half of the functionality of this project. The Cayenne Dashboard needs to be setup to provide the rest of the functionality. The following instructions will show you how to add each of the widgets required for this Home Security project.


Arduino Ethernet - Master Switch

The master switch allows me to turn the security system on and off. When I turn the MASTER SWITCH ON, the laser beam will turn on, and the sensors will start monitoring the area for intruders. This widget is NOT associated with a physical switch/sensor on the Arduino - it uses virtual channel 0. We need to add the Master switch to the dashboard:


  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. Digital Output - Control a Digital Output
  6. Widget Name: Master On Off Switch
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V0
  10. Choose Widget: Button
  11. Choose Icon: Valve
  12. Step2: Add Actuator
We will add a trigger later to get this button to automatically turn the Laser beam on.


 
 
 

Arduino Ethernet - PIR Sensor

This sensor will be used to detect movement in the room. If a person walks into the room, this sensor will detect movement, and will trigger a message to be played on the Grove Serial MP3 player. The message will aim to get the person to identify themselves. They identify themselves by placing their RFID tag in close proximity to the Grove RFID reader. If the tag is valid, a "Welcome home" message is played on the Grove MP3 player. If a valid tag is not presented to the reader within 10 seconds, an Alarm will go off ("Alarm sound" played on Grove MP3 player.)

The PIR sensor is connected to digital Pin 6 of the Arduino, however, it is mapped to virtual pin 1 for better synchronisation with the Cayenne dashboard. This was done to capture ALL detections - as the PIR sensor could change from a LOW to HIGH and back to LOW state in between a Cayenne state check - and therefore, Cayenne could miss this motion detection.. Therefore we need to assign the PIR sensor to a virtual channel in the following way:
  1. Add New
  2. Device/Widget
  3. Sensors
  4. Motion
  5. Digital Motion Sensor - Motion Detector
  6. Widget Name: PIR sensor
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V1
  10. Choose Widget: 2-State Display
  11. Choose Icon: Light
  12. Step2: Add Sensor
  13. Select Settings from the PhotoResistor
  14. Choose Display: Value
  15. Save

 
 
 

Arduino Ethernet - Photoresistor

This sensor will be used with the laser beam to create a laser tripwire. If the sensor detects a change in light levels (drops below the threshold), it will activate the laser trigger button on the dashboard. The person will then be required to identify themselves etc etc (similar to the motion detection by the PIR sensor). The photoresistor widget will display the raw analog reading from the sensor (connected to A2), but is associated with virtual channel 2. I used a virtual channel for more control over this sensor. To add the Photoresistor to the dashboard:

  1. Add New
  2. Device/Widget
  3. Sensors
  4. Luminosity
  5. Photoresistor - Luminosity sensor
  6. Widget Name: PhotoResistor
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V2
  10. Choose Widget: Value
  11. Choose Icon: Light
  12. Step2: Add Sensor


 
 
 

Arduino Ethernet - Laser Trigger

The laser trigger is just an indicator that someone tripped the laser beam. The state of this widget is used to notify the Seeeduino that a presence has been detected. This widget is associated with virtual pin 4 on the Arduino UNO with Ethernet.

  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. Digital Output - Control a Digital Output
  6. Widget Name: Laser Trigger
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V4
  10. Choose Widget: Button
  11. Choose Icon: Lock
  12. Step2: Add Actuator


 
 
 

Arduino Ethernet - Laser Threshold

The laser threshold is used to manually configure the light level at which the laser trigger will trip. When the photoresistor value drops below the threshold value, the laser trigger icon will activate. This allows the threshold value to be updated from the Cayenne dashboard, rather than having to manually adjust the value in the Arduino code. Also, this threshold can be set remotely, in that you don't have to be near the Arduino to change this value. A very useful feature of this Security system. This widget is associated with virtual pin 5 on the Arduino UNO with Ethernet.

  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. PWM Output - Control a PWM Output
  6. Widget Name: Laser Threshold
  7. Select Device: Arduino Ethernet
  8. Connectivity: Virtual
  9. Pin: V5
  10. Choose Widget: Slider
  11. Slider Min Value: 0
  12. Slider Max Value: 10
  13. Step2: Add Actuator
The max value of the slider is 10 - due to a current bug in the Cayenne software. Once resolved, this value (as well as the relevant Arduino code) will need to be updated.


 
 
 

Seeeduino Cloud - Presence Detected

The presence detected widget is there to notify the Seeeduino Cloud that a presence has been detected on the Arduino Uno with Ethernet shield. When the PIR sensor detects movement or if the laser tripwire is tripped, Cayenne will change the state of the Presence Detected widget from LOW to HIGH. This is used within the Seeeduino Cloud to trigger the message "Place your keys on the Mat"
. If a valid RFID tag is read by the Grove RFID reader, then this widget's state will change back from HIGH to LOW, and the MasterSwitch will be deactivated - turning the Security system off. This widget is associated with Virtual pin 6 on the Seeeduino Cloud.

  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. Digital Output - Control a Digital Output
  6. Widget Name: Presence Detected
  7. Select Device: Seeeduino Cloud
  8. Connectivity: Virtual
  9. Pin: V6
  10. Choose Widget: Button
  11. Choose Icon: Lock
  12. Step2: Add Actuator


 
 
 

Seeeduino Cloud - Intruder Alert

If a valid RFID tag is not read by the Grove RFID reader within 10 seconds of a presence detection event, an alarm will sound, and this widget will be activated. This will trigger a notification event - to notify me of the unauthorised intrusion - via SMS or email. I will also have a visual indicator on the Cayenne dashboard that an intrusion has taken place. This widget is associated with Virtual pin 7 on the Seeeduino Cloud.

  1. Add New
  2. Device/Widget
  3. Actuators
  4. Generic
  5. Digital Output - Control a Digital Output
  6. Widget Name: Laser Trigger
  7. Select Device: Seeeduino Cloud
  8. Connectivity: Virtual
  9. Pin: V7
  10. Choose Widget: Button
  11. Choose Icon: Thermometer
  12. Step2: Add Actuator


 
 
 

Seeeduino Cloud - Laser Beam

The laser beam widget was created to allow for full control over the laser beam. The laser beam can be turned on or off from the Cayenne dashboard, and a connected to digital pin 7 on the Seeeduino Cloud.


  1. Add New
  2. Device/Widget
  3. Actuators
  4. Light
  5. Light Switch - Turn On/Off a Light
  6. Widget Name: xLaser Beam
  7. Select Device: Seeeduino Cloud
  8. Connectivity: Digital
  9. Pin: D7
  10. Choose Widget: Button
  11. Choose Icon: Light
  12. Step2: Add Actuator


 
 
 
 

Cayenne Triggers

Now that all of the widgets have been added to the Dashboard, there is just one more step to complete the Security System. We need to setup the triggers. These triggers provide a level of automation that is easy to create within Cayenne, but would be very complicated otherwise. I set my triggers up as per the table below. Each row represents one of the triggers within my Cayenne dashboard. If you would like to see an example of how to add a trigger - please have a look at the video at the top of this tutorial.  


 
 
 
 
 
 

Concluding comments

I used many different elements to put this home/office security project together - Multiple Arduinos were connected to the internet, both controlled by a web/smart phone app, cross-communication/synchronisation between the Arduinos, and the use of multiple sensors and modules including a laser beam !
 
This was way more than just a simple PIR sense and alarm project. I now have a personalised greeting and reminder system when I walk in the door. Everyone else has their own personalised greeting. I can enable my Security System remotely, from two blocks away, and if I wanted to - I could enable it from the other side of the world. I know instantly when someone has entered my house/office.... with an SMS alert straight to my phone.
 
This project could easily be extended:

  1. Press a button on my phone to manually trigger/play a specific message/sound/song
  2. Take a picture of the intruder
  3. Introduce fire or leak detection aswell
  4. Add other environmental sensors - Temperature / Humidity
  5. Connect it to lamp/light - creating a security light
I am sure you can think of more things I could do with this system. In fact, why don't you mention your ideas in the comments below.
 
Cayenne was instrumental in getting this project to work. I don't think I would know where to start if I had to do this project without this cool IoT platform. I think I will definitely be trying out a few more projects using Cayenne, and should you want to do the same, then please make sure to join Cayenne Beta:
 
Here is the link you need to get to the right place: Cayenne Beta Link

 

If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.

             

ScottC 30 Aug 15:42
alarm  arduino  arduinobasics  cayenne  laser  mp3  mydevices  pir  rfid  security  sms  tutorial  

Arduino LED Light Box

Description

Long straight lines of LED luminescence is nice, but sometimes you may want to light up something that has an unusual shape, or is not so linear. This is where the 12mm diffused flat digital RGB LED Pixels can come into play. This cool strand of 25 RGB LED pixels fit nicely into 12mm pre-drilled holes of any material you like.

This tutorial is dedicated to making a LED Light Box. I wanted the box to be equally as interesting during the day as it was at night. If you decide you make your own, feel free to be as creative as you want !! However, if you lack artistic acumen, you may need to source a minion or two.


 

Arduino Libraries and IDE

Before you start to hook up any components, upload the following sketch to the Arduino microcontroller. I am assuming that you already have the Arduino IDE installed on your computer. If not, the IDE can be downloaded from here.

The FastLED library is useful for simplifying the code for programming the RGB LED pixels. The latest "FastLED library" can be downloaded from here. I used FastLED library version 3.0.3 in this project.

If you have a different LED strip or your RGB LED pixels have a different chipset, make sure to change the relevant lines of code to accomodate your hardware. I would suggest you try out a few of the FastLED library examples before using the code below, so that you become more familiar with the library, and will be better equipped to make the necessary changes.

If you have a single strand of 25 RGB LED pixels with the WS8201 chipset, then you will not have to make any modification below.


 

ARDUINO CODE:

Arduino Code Description

The code above will generate a randomised raindrop pattern on the Arduino LED Light box, however I have written code for a few more LED animations. These animations were written specifically for this light-box setup. In other words, once you have hooked everything up, you will be able to upload these other LED animations to the Arduino board without any further modification to the hardware/wiring, and yet experience a totally different light effect. You can find the code for the other animation effects by clicking on the links below:

  1. Breathing effect
  2. Ripple effect
  3. Clock effect
  4. Rotation effect
  5. Sweep effect
  6. Spiral effect
  7. Lightning effect
  8. Paparazzi in the Rain effect

Hooking it up:

Power requirements

Each LED pixel can draw up to 60 milliamps at maximum brightness (white). ie. 20 mA for each colour (red, green and blue). Therefore you should not try to power the LED strand directly from the Arduino, because the strand will draw too much current and damage the microcontroller(and possibly your USB port too). The LED strand will therefore need to be powered by a separate power supply. The power supply must supply the correct voltage (5V DC) and must also be able to supply sufficient current (1.5A or greater per strand of 25 LEDs).

Excessive voltage will damage or destroy your LED pixel strand. The LEDs will only draw as much current as they need, however your power supply must provide at least 1.5A or greater for each strand. If you chain two strands together, you will need a 5V 3A power supply.

RGB LED pixel strand connection

There are 25 LED pixels per strand. Four of the wires at each end of the strand are terminated with a JST connector. The red wire is for power (VCC), blue wire for ground (GND), yellow wire is for Data, and green wire for Clock. A spare red wire (VCC) and a spare blue wire (GND) are attached to the ends of each strand for convenience, however, I did not use either. Please double check the colour of your wires... they may be different.

If you want to attach the LED strand to a breadboard, you can cut the JST connector off and use the LED pixel strand wires. Alternatively, if you would prefer to preserve the JST connector, you can simply insert jumper wires (or some male header pins) into the JST connector, and then plug them into the breadboard as required.

Each LED pixel is individually controllable using two pins on your Arduino. The strand is directional. i.e. There is an INPUT side and an OUTPUT side. The strand should be connected such that wires from the microcontroller are attached to the INPUT side of the first LED pixel. The arrows on each LED show the direction of data flow from INPUT to OUTPUT. The arrow on the first LED pixel should be pointing towards the second LED pixel, NOT towards the breadboard.

Other considerations

As a precaution, you should use a large capacitor across the + and - terminals of the power supply to prevent the initial onrush of current from damaging the RGB LED pixels. I used a 4700uF 16V Electrolytic capacitor for this purpose. According to Adafruit, a 1000uF 6.3V capacitor (or higher) will also do the trick. You may also want to consider a 330 ohm resistor between the Arduino Digital pin and the strand's DATA pin.

If you want to power the Arduino using the regulated 5V external power supply. Disconnect the USB cable from the Arduino, and then connect the positive terminal of the power supply to the 5V pin on the Arduino. Be warned however, that excess voltage at this pin could damage your Arduino, because the 5V regulator will be bypassed.
 
Providing the USB cable is NOT connected to the Arduino, it should now be safe to plug the power supply into the wall. This setup will allow you to power the RGB LED pixel strand and the Arduino using the same power supply.
 
WARNING: Never change any connections while the circuit is powered.

For more information about these RGB LED pixel strands, you may want to visit the Adafruit site. Adafruit was the source for most of these RGB LED pixel Strand precautions.


Fritzing diagram

The following diagram demonstrates how to connect the RGB LED pixel Strand to the Arduino and to the External 5V power supply.


This diagram was created using Fritzing


Connection Instructions

These instructions will help to guide you through the process of connecting your RGB LED pixel strand to the Arduino, and to the external power supply. The instructions assume that you will be powering the Arduino via a USB cable.



LightBox assembly

You will need to drill a 12mm hole into the craft timber box for each LED on the strand. It is worth taking the time to make accurate measurements before drilling the holes.
 
I made 12 holes for the outside circle pattern (12cm diameter), 6 holes for the inside circle pattern (8cm diameter), and a hole in the centre. I also made two holes at the front of the box, two on the left side, and two on the right side. I made one last hole at the back of the box for the 2.1mm DC power line socket.
 
Therefore you should have a total of 26 holes in the box. 25 of the holes are for the RGB LED pixel LEDs and one for the external power supply socket.

The lid of the box is about 19.5cm x 14.5cm long, which makes for a very tight squeeze. Probably too tight, because you have to account for the inner dimensions of the box. The inside of the box is used to house the Arduino, breadboard, the chipset side of the LEDs and cables/components. The inner dimensions of the box are 18cm x 13cm. Therefore, the housing for the LED chipset PCB (1.8cm x 2.5cm) prevented the box from closing. I used a Dremel to carve out the space required to close the lid.

Each LED is approximately 8cm apart on the strand, however, if you are really keen, you could cut the wires and extend them to any distance you require. But keep in mind that each LED is mounted on a small PCB (with a WS2801 chipset).You will therefore need to leave a minimum of 2cm between each 12mm hole to accomodate the size of the PCB+LED. If you plan carefully, you can probably squeeze a couple of LEDs within a distance of 1cm... but I would recommend that you give yourself a bit more room, because the PCBs are not square, and there is a good chance that you will have to start all over again.

In hindsight, I could have made the circle patterns a bit smaller, however I don't know if I could have packed these LEDs any closer. The diameter of the inner circle pattern must be at least 2cm smaller than the outer circle pattern. So I think "a bigger box" would have been the best option.

Once all of the holes have been drilled, paint and decorate the box to suit your style.

When the paint is dry, insert the LEDs into the drilled holes in number order.
You can see the end result below.



Project Pictures

These pictures show the Light box after it has been drilled and painted. The LEDs have been inserted into their respective holes, and all wires + Arduino + breadboard are hidden within the box.





Concluding comments

Once you start writing LED animations for the RGB LED pixel Lightbox, it is very hard to stop. The colour combinations



If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.

             

This project would not have been possible without OpenLab's collaborative effort.
Please visit their site for more cool projects.



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

Arduino LED Light Box

Description

Long straight lines of LED luminescence is nice, but sometimes you may want to light up something that has an unusual shape, or is not so linear. This is where the 12mm diffused flat digital RGB LED Pixels can come into play. This cool strand of 25 NeoPixels fit nicely into 12mm pre-drilled holes of any material you like.

This tutorial is dedicated to making a LED Light Box. I wanted the box to be equally as interesting during the day as it was at night. If you decide you make your own, feel free to be as creative as you want !! However, if you lack artistic acumen, you may need to source a minion or two.


 

Arduino Libraries and IDE

Before you start to hook up any components, upload the following sketch to the Arduino microcontroller. I am assuming that you already have the Arduino IDE installed on your computer. If not, the IDE can be downloaded from here.

The FastLED library is useful for simplifying the code for programming the NeoPixels. The latest "FastLED library" can be downloaded from here. I used FastLED library version 3.0.3 in this project.

If you have a different LED strip or your NeoPixels have a different chipset, make sure to change the relevant lines of code to accomodate your hardware. I would suggest you try out a few of the FastLED library examples before using the code below, so that you become more familiar with the library, and will be better equipped to make the necessary changes.

If you have a single strand of 25 Neopixels with the WS8201 chipset, then you will not have to make any modification below.


 

ARDUINO CODE:

Arduino Code Description

The code above will generate a randomised raindrop pattern on the NeoPixel LED Light box, however I have written code for a few more LED animations. These animations were written specifically for this light-box setup. In other words, once you have hooked everything up, you will be able to upload these other LED animations to the Arduino board without any further modification to the hardware/wiring, and yet experience a totally different light effect. You can find the code for the other animation effects by clicking on the links below:

  1. Breathing effect
  2. Ripple effect
  3. Clock effect
  4. Rotation effect
  5. Sweep effect
  6. Spiral effect
  7. Lightning effect
  8. Paparazzi in the Rain effect

Hooking it up:

Power requirements

Each Neopixel LED can draw up to 60 milliamps at maximum brightness (white). ie. 20 mA for each colour (red, green and blue). Therefore you should not try to power the LED strand directly from the Arduino, because the strand will draw too much current and damage the microcontroller(and possibly your USB port too). The LED strand will therefore need to be powered by a separate power supply. The power supply must supply the correct voltage (5V DC) and must also be able to supply sufficient current (1.5A or greater per strand of 25 LEDs).

Excessive voltage will damage or destroy your Neopixel strand. The LEDs will only draw as much current as they need, however your power supply must provide at least 1.5A or greater for each strand. If you chain two strands together, you will need a 5V 3A power supply.

Neopixel strand connection

There are 25 Neopixel LEDs per strand. Four of the wires at each end of the strand are terminated with a JST connector. The red wire is for power (VCC), blue wire for ground (GND), yellow wire is for Data, and green wire for Clock. A spare red wire (VCC) and a spare blue wire (GND) are attached to the ends of each strand for convenience, however, I did not use either. Please double check the colour of your wires... they may be different.

If you want to attach the LED strand to a breadboard, you can cut the JST connector off and use the Neopixel strand wires. Alternatively, if you would prefer to preserve the JST connector, you can simply insert jumper wires (or some male header pins) into the JST connector, and then plug them into the breadboard as required.

Each neopixel LED is individually controllable using two pins on your Arduino. The strand is directional. i.e. There is an INPUT side and an OUTPUT side. The strand should be connected such that wires from the microcontroller are attached to the INPUT side of the first neopixel. The arrows on each LED show the direction of data flow from INPUT to OUTPUT. The arrow on the first NeoPixel should be pointing towards the second NeoPixel, NOT towards the breadboard.

Other considerations

As a precaution, you should use a large capacitor across the + and - terminals of the power supply to prevent the initial onrush of current from damaging the Neopixels. I used a 4700uF 16V Electrolytic capacitor for this purpose. According to Adafruit, a 1000uF 6.3V capacitor (or higher) will also do the trick. You may also want to consider a 330 ohm resistor between the Arduino Digital pin and the strand's DATA pin.

If you want to power the Arduino using the regulated 5V external power supply. Disconnect the USB cable from the Arduino, and then connect the positive terminal of the power supply to the 5V pin on the Arduino. Be warned however, that excess voltage at this pin could damage your Arduino, because the 5V regulator will be bypassed.
 
Providing the USB cable is NOT connected to the Arduino, it should now be safe to plug the power supply into the wall. This setup will allow you to power the Neopixel strand and the Arduino using the same power supply.
 
WARNING: Never change any connections while the circuit is powered.

For more information about these NeoPixel strands, you may want to visit the Adafruit site. Adafruit was the source for most of these NeoPixel Strand precautions.


Fritzing diagram

The following diagram demonstrates how to connect the NeoPixel Strand to the Arduino and to the External 5V power supply.


This diagram was created using Fritzing


Connection Instructions

These instructions will help to guide you through the process of connecting your NeoPixel strand to the Arduino, and to the external power supply. The instructions assume that you will be powering the Arduino via a USB cable.



LightBox assembly

You will need to drill a 12mm hole into the craft timber box for each LED on the strand. It is worth taking the time to make accurate measurements before drilling the holes.
 
I made 12 holes for the outside circle pattern (12cm diameter), 6 holes for the inside circle pattern (8cm diameter), and a hole in the centre. I also made two holes at the front of the box, two on the left side, and two on the right side. I made one last hole at the back of the box for the 2.1mm DC power line socket.
 
Therefore you should have a total of 26 holes in the box. 25 of the holes are for the Neopixel LEDs and one for the external power supply socket.

The lid of the box is about 19.5cm x 14.5cm long, which makes for a very tight squeeze. Probably too tight, because you have to account for the inner dimensions of the box. The inside of the box is used to house the Arduino, breadboard, the chipset side of the LEDs and cables/components. The inner dimensions of the box are 18cm x 13cm. Therefore, the housing for the LED chipset PCB (1.8cm x 2.5cm) prevented the box from closing. I used a Dremel to carve out the space required to close the lid.

Each LED is approximately 8cm apart on the strand, however, if you are really keen, you could cut the wires and extend them to any distance you require. But keep in mind that each LED is mounted on a small PCB (with a WS2801 chipset).You will therefore need to leave a minimum of 2cm between each 12mm hole to accomodate the size of the PCB+LED. If you plan carefully, you can probably squeeze a couple of LEDs within a distance of 1cm... but I would recommend that you give yourself a bit more room, because the PCBs are not square, and there is a good chance that you will have to start all over again.

In hindsight, I could have made the circle patterns a bit smaller, however I don't know if I could have packed these LEDs any closer. The diameter of the inner circle pattern must be at least 2cm smaller than the outer circle pattern. So I think "a bigger box" would have been the best option.

Once all of the holes have been drilled, paint and decorate the box to suit your style.

When the paint is dry, insert the LEDs into the drilled holes in number order.
You can see the end result below.



Project Pictures

These pictures show the Light box after it has been drilled and painted. The LEDs have been inserted into their respective holes, and all wires + Arduino + breadboard are hidden within the box.





Concluding comments

Once you start writing LED animations for the NeoPixel Lightbox, it is very hard to stop. The colour combinations



If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.

             

This project would not have been possible without OpenLab's collaborative effort.
Please visit their site for more cool projects.



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

Say Hello

I have just launched my guest book on my blog.

You will notice a new menu at the top of the blog which reads "Guest Book".
This is just a place for people to say hello, share their project with me, or provide any other feedback. You can either leave a video message, or a traditional written message, either (or both) is fine.

Thank you very much for visiting. Knowing that people like you are actually reading my work is incentive enough to keep on going. Please tell me if you don't like something, or if you do like something, tell me that too.

Sometimes it is hard to know if I am on the right track and whether there is any benefit in providing this information.
Thank you very much for dropping by.
Please come again soon :)

Visit my Guest book here

ScottC 11 Oct 15:11

MT8870 DTMF - Dual Tone Multi Frequency Decoder

Project Description

We will be using an MT8870 DTMF module with an Arduino UNO to control a small servo motor in this project. The DTMF module gives the Arduino super-powers and allows you to control the Servo motor in so many ways. For example, this tutorial will show you how to control the servo motor using:
  • a YouTube Video
  • a voice recorder
  • A web application (Online tone generator)
  • A smart phone app (DTMF Pad)
  • A touch-tone phone to cell-phone call
All of these control methods will take advantage of the same exact Arduino code/sketch. But how???
The MT8870 DTMF decoder is quite a neat little module that allows you incorporate DTMF technology into your arduino projects. DTMF stands for Dual-Tone Multi-Frequency. DTMF tones are commonly associated with touch-tone phones and other telecommunication systems. When you press the number "1" on a touch-tone phone, two sine waves with frequencies: 697Hz and 1209Hz are combined to produce a unique DTMF signal which can be transmitted through the phone line. The MT8870 DTMF module can take this signal as an input, and decode it to produce a binary output.
 
 

 
The DTMF module does not care how you produce the DTMF tone. However, if it receives this tone, it will decode it. We can take advantage of this feature to supply the module with tones from different sources. The module has a 3.5mm port for line input. Providing you can connect your DTMF source to this line input in some way, it should work. I must warn you, however that this is a line input and NOT a microphone input. If you wanted to use a microphone, you will need to boost or amplify the signal before sending it to the DTMF module.
 
You will need the following parts for this project
 

Parts Required:

Software/Apps Required

Arduino Sketch


Upload the following sketch to the Arduino.
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ================================================================================================================================================== Project: MT8870 DTMF Servo sketch Author: Scott C Created: 4th August 2015 Arduino IDE: 1.6.4 Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html Description: This project will allow you to control a Servo motor using an Arduino UNO and a MT8870 DTMF Module. The DTMF signal is received through the 3.5mm port of the DTMF module and is decoded. We will use the decoded output to control the position of the Servo. A SG-5010 Servo motor was used in this project. ===================================================================================================================================================== *///This sketch uses the Servo library that comes with the Arduino IDE #include <Servo.h> //Global variables----------------------------------------------------------------------------------------- Servo SG5010; // The SG5010 variable provides Servo functionality int servoPosition = 0; // The servoPosition variable will be used to set the position of the servo byte DTMFread; // The DTMFread variable will be used to interpret the output of the DTMF module. const int STQ = 3; // Attach DTMF Module STQ Pin to Arduino Digital Pin 3 const int Q4 = 4; // Attach DTMF Module Q4 Pin to Arduino Digital Pin 4 const int Q3 = 5; // Attach DTMF Module Q3 Pin to Arduino Digital Pin 5 const int Q2 = 6; // Attach DTMF Module Q2 Pin to Arduino Digital Pin 6 const int Q1 = 7; // Attach DTMF Module Q1 Pin to Arduino Digital Pin 7 /*========================================================================================================= setup() : will setup the Servo, and prepare the Arduino to receive the MT8700 DTMF module's output. ========================================================================================================== */void setup() { SG5010.attach(9); // The Servo signal cable will be attached to Arduino Digital Pin 9 SG5010.write(servoPosition); // Set the servo position to zero. //Setup the INPUT pins on the Arduino pinMode(STQ, INPUT); pinMode(Q4, INPUT); pinMode(Q3, INPUT); pinMode(Q2, INPUT); pinMode(Q1, INPUT);} /*========================================================================================================= loop() : Arduino will interpret the DTMF module output and position the Servo accordingly ========================================================================================================== */void loop() { if(digitalRead(STQ)==HIGH){ //When a DTMF tone is detected, STQ will read HIGH for the duration of the tone. DTMFread=0; if(digitalRead(Q1)==HIGH){ //If Q1 reads HIGH, then add 1 to the DTMFread variable DTMFread=DTMFread+1; } if(digitalRead(Q2)==HIGH){ //If Q2 reads HIGH, then add 2 to the DTMFread variable DTMFread=DTMFread+2; } if(digitalRead(Q3)==HIGH){ //If Q3 reads HIGH, then add 4 to the DTMFread variable DTMFread=DTMFread+4; } if(digitalRead(Q4)==HIGH){ //If Q4 reads HIGH, then add 8 to the DTMFread variable DTMFread=DTMFread+8; } servoPosition = DTMFread * 8.5; //Set the servoPosition varaible to the combined total of all the Q1 to Q4 readings. Multiply by 8.5 to amplify the servo rotation. } SG5010.write(servoPosition); //Set the servo's position according to the "servoPosition" variable. }


 
 
 

Fritzing Sketch


Connect the Arduino to the MT8870 DTMF module, and to a Servo.
Use the following Fritzing sketch as a guide.
 
(Click the image above to enlarge it)



Discussion


You will need to connect a cable from the DTMF module's 3.5mm port to that of your smart phone, computer, voice recorder or any other DTMF source of your choice.
 

 

When you power up your Arduino, the Servo motor should turn all the way to the left to it's zero position. Once the DTMF module receives a DTMF signal, it will identify the relevant frequecies as described in the table at the beginning of this tutorial, and produce a binary like output. You will notice the DTMF module's onboard LEDs light up when a tone is detected. Onboard LED (D5) will turn on for the length of the DTMF tone it just received, and turn off when the tone has stopped. On the other hand, the onboard LEDs (D1 to D4) will light up depending on the tone received, and will remain lit until the module receives another tone. The onboard LEDs are a visual representation of the voltages applied to the DTMF module's pins (Q1 to Q4, and STQ). Q1 matches D1, Q2 matches D2 etc etc. and STQ matches D5.
 
You will notice that there are two STQ pins on the DTMF module. The STQ pin that is closest to Q4 will only go high when a DTMF tone is detected, and will remain high for the duration of the tone. The other STQ pin is the exact opposite. It will switch LOW when a tone is received and remain LOW for the duration of the tone. When there is no tone, this STQ pin will remain HIGH. The table below provides a summary of the DTMF module outputs, with a blue box representing a voltage applied to that pin (HIGH), whereas a black box indicates no voltage applied (LOW).


 
In order to follow this project, you need a source of DTMF tones. You can produce DTMF tones using a touch-tone phone, or through the use of a DTMF Pad app. If you are feeling creative, you can create a DTMF song/tune like the one I posted on YouTube. You can see the video below:
 

 
As you can see from the video, I also recorded the DTMF tune onto a voice recorder, and was able to control the servo that way. If you are not feeling creative, you can visit this website to create DTMF tones from your browser.

Concluding comments


This project was very fun, and shows some novel ways to control your Arduino. After completing the project, I realised that I could use this module to alert me when new emails or messages arrive on my phone or computer. If you have the ability to change the email or message notification sound to a DTMF tone, you should be able to get the module and Arduino to respond accordingly. Oh well, maybe I'll save that project for another day.
 
If this project helped you in anyway or if you use my code within your project, please let me know in the comments below. I would be interested to see what you did.


If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.


 
 
             

 



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

MT8870 DTMF - Dual Tone Multi Frequency Decoder

Project Description

We will be using an MT8870 DTMF module with an Arduino UNO to control a small servo motor in this project. The DTMF module gives the Arduino super-powers and allows you to control the Servo motor in so many ways. For example, this tutorial will show you how to control the servo motor using:
  • a YouTube Video
  • a voice recorder
  • A web application (Online tone generator)
  • A smart phone app (DTMF Pad)
  • A touch-tone phone to cell-phone call
All of these control methods will take advantage of the same exact Arduino code/sketch. But how???
The MT8870 DTMF decoder is quite a neat little module that allows you incorporate DTMF technology into your arduino projects. DTMF stands for Dual-Tone Multi-Frequency. DTMF tones are commonly associated with touch-tone phones and other telecommunication systems. When you press the number "1" on a touch-tone phone, two sine waves with frequencies: 697Hz and 1209Hz are combined to produce a unique DTMF signal which can be transmitted through the phone line. The MT8870 DTMF module can take this signal as an input, and decode it to produce a binary output.
 
 

 
The DTMF module does not care how you produce the DTMF tone. However, if it receives this tone, it will decode it. We can take advantage of this feature to supply the module with tones from different sources. The module has a 3.5mm port for line input. Providing you can connect your DTMF source to this line input in some way, it should work. I must warn you, however that this is a line input and NOT a microphone input. If you wanted to use a microphone, you will need to boost or amplify the signal before sending it to the DTMF module.
 
You will need the following parts for this project
 

Parts Required:

Software/Apps Required

Arduino Sketch


Upload the following sketch to the Arduino.
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ================================================================================================================================================== Project: MT8870 DTMF Servo sketch Author: Scott C Created: 4th August 2015 Arduino IDE: 1.6.4 Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html Description: This project will allow you to control a Servo motor using an Arduino UNO and a MT8870 DTMF Module. The DTMF signal is received through the 3.5mm port of the DTMF module and is decoded. We will use the decoded output to control the position of the Servo. A SG-5010 Servo motor was used in this project. ===================================================================================================================================================== *///This sketch uses the Servo library that comes with the Arduino IDE #include <Servo.h> //Global variables----------------------------------------------------------------------------------------- Servo SG5010; // The SG5010 variable provides Servo functionality int servoPosition = 0; // The servoPosition variable will be used to set the position of the servo byte DTMFread; // The DTMFread variable will be used to interpret the output of the DTMF module. const int STQ = 3; // Attach DTMF Module STQ Pin to Arduino Digital Pin 3 const int Q4 = 4; // Attach DTMF Module Q4 Pin to Arduino Digital Pin 4 const int Q3 = 5; // Attach DTMF Module Q3 Pin to Arduino Digital Pin 5 const int Q2 = 6; // Attach DTMF Module Q2 Pin to Arduino Digital Pin 6 const int Q1 = 7; // Attach DTMF Module Q1 Pin to Arduino Digital Pin 7 /*========================================================================================================= setup() : will setup the Servo, and prepare the Arduino to receive the MT8700 DTMF module's output. ========================================================================================================== */void setup() { SG5010.attach(9); // The Servo signal cable will be attached to Arduino Digital Pin 9 SG5010.write(servoPosition); // Set the servo position to zero. //Setup the INPUT pins on the Arduino pinMode(STQ, INPUT); pinMode(Q4, INPUT); pinMode(Q3, INPUT); pinMode(Q2, INPUT); pinMode(Q1, INPUT);} /*========================================================================================================= loop() : Arduino will interpret the DTMF module output and position the Servo accordingly ========================================================================================================== */void loop() { if(digitalRead(STQ)==HIGH){ //When a DTMF tone is detected, STQ will read HIGH for the duration of the tone. DTMFread=0; if(digitalRead(Q1)==HIGH){ //If Q1 reads HIGH, then add 1 to the DTMFread variable DTMFread=DTMFread+1; } if(digitalRead(Q2)==HIGH){ //If Q2 reads HIGH, then add 2 to the DTMFread variable DTMFread=DTMFread+2; } if(digitalRead(Q3)==HIGH){ //If Q3 reads HIGH, then add 4 to the DTMFread variable DTMFread=DTMFread+4; } if(digitalRead(Q4)==HIGH){ //If Q4 reads HIGH, then add 8 to the DTMFread variable DTMFread=DTMFread+8; } servoPosition = DTMFread * 8.5; //Set the servoPosition varaible to the combined total of all the Q1 to Q4 readings. Multiply by 8.5 to amplify the servo rotation. } SG5010.write(servoPosition); //Set the servo's position according to the "servoPosition" variable. }


 
 
 

Fritzing Sketch


Connect the Arduino to the MT8870 DTMF module, and to a Servo.
Use the following Fritzing sketch as a guide.
 
(Click the image above to enlarge it)



Discussion


You will need to connect a cable from the DTMF module's 3.5mm port to that of your smart phone, computer, voice recorder or any other DTMF source of your choice.
 

 

When you power up your Arduino, the Servo motor should turn all the way to the left to it's zero position. Once the DTMF module receives a DTMF signal, it will identify the relevant frequecies as described in the table at the beginning of this tutorial, and produce a binary like output. You will notice the DTMF module's onboard LEDs light up when a tone is detected. Onboard LED (D5) will turn on for the length of the DTMF tone it just received, and turn off when the tone has stopped. On the other hand, the onboard LEDs (D1 to D4) will light up depending on the tone received, and will remain lit until the module receives another tone. The onboard LEDs are a visual representation of the voltages applied to the DTMF module's pins (Q1 to Q4, and STQ). Q1 matches D1, Q2 matches D2 etc etc. and STQ matches D5.
 
You will notice that there are two STQ pins on the DTMF module. The STQ pin that is closest to Q4 will only go high when a DTMF tone is detected, and will remain high for the duration of the tone. The other STQ pin is the exact opposite. It will switch LOW when a tone is received and remain LOW for the duration of the tone. When there is no tone, this STQ pin will remain HIGH. The table below provides a summary of the DTMF module outputs, with a blue box representing a voltage applied to that pin (HIGH), whereas a black box indicates no voltage applied (LOW).


 
In order to follow this project, you need a source of DTMF tones. You can produce DTMF tones using a touch-tone phone, or through the use of a DTMF Pad app. If you are feeling creative, you can create a DTMF song/tune like the one I posted on YouTube. You can see the video below:
 

 
As you can see from the video, I also recorded the DTMF tune onto a voice recorder, and was able to control the servo that way. If you are not feeling creative, you can visit this website to create DTMF tones from your browser.

Concluding comments


This project was very fun, and shows some novel ways to control your Arduino. After completing the project, I realised that I could use this module to alert me when new emails or messages arrive on my phone or computer. If you have the ability to change the email or message notification sound to a DTMF tone, you should be able to get the module and Arduino to respond accordingly. Oh well, maybe I'll save that project for another day.
 
If this project helped you in anyway or if you use my code within your project, please let me know in the comments below. I would be interested to see what you did.


If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.


 
 
             

 



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

NeoPixel Heart Beat Display


Project Description


In this project, your heart will control a mesmerising LED sequence on a 5 metre Neopixel LED strip with a ws2812B chipset. Every heart beat will trigger a LED animation that will keep you captivated and attached to your Arduino for ages. The good thing about this project is that it is relatively easy to set up, and requires no soldering. The hardest part is downloading and installing the FastLED library into the Arduino IDE, but that in itself is not too difficult. The inspiration and idea behind this project came from Ali Murtaza, who wanted to know how to get an LED strip to pulse to his heart beat.
 
Have a look at the video below to see this project in action.
 
 
 

The Video


 


 
 

Parts Required:


 

Power Requirements

Before you start any LED strip project, the first thing you will need to think about is POWER. According to the Adafruit website, each individual NeoPixel LED can draw up to 60 milliamps at maximum brightness - white. Therefore the amount of current required for the entire strip will be way more than your Arduino can handle. If you try to power this LED strip directly from your Arduino, you run the risk of damaging not only your Arduino, but your USB port as well. The Arduino will be used to control the LED strip, but the LED strip will need to be powered by a separate power supply. The power supply you choose to use is important. It must provide the correct voltage, and must able to supply sufficient current.
 

Operating Voltage (5V)

The operating voltage of the NeoPixel strip is 5 volts DC. Excessive voltage will damage/destroy your NeoPixels.

Current requirements (9.0 Amps)

OpenLab recommend the use of a 5V 10A power supply. Having more Amps is OK, providing the output voltage is 5V DC. The LEDs will only draw as much current as they need. To calculate the amount of current this 5m strip can draw with all LEDs turned on at full brightness - white:

30 NeoPixel LEDs x 60mA x 5m = 9000mA = 9.0 Amps for a 5 metre strip.

Therefore a 5V 10A power supply would be able to handle the maximum current (9.0 Amps) demanded by a 5m NeoPixel strip containing a total of 150 LEDs.
 
 


Arduino Libraries and IDE


Before you start to hook up any components, upload the following sketch to the Arduino microcontroller. I am assuming that you already have the Arduino IDE installed on your computer. If not, the IDE can be downloaded from here.
 
The FastLED library is useful for simplifying the code for programming the NeoPixels. The latest "FastLED library" can be downloaded from here. I used FastLED library version 3.0.3 in this project.
 
If you have a different LED strip or your NeoPixels have a different chipset, make sure to change the relevant lines of code to accomodate your hardware. I would suggest you try out a few of the FastLED library examples before using the code below, so that you become more familiar with the library, and will be better equipped to make the necessary changes. If you have a 5 metre length of the NeoPixel 30 LED/m strip with the ws2812B chipset, then you will not have to make any modification below.
 

ARDUINO CODE:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* ================================================================================================ Project: NeoPixel Heart Beat Display Neopixel chipset: ws2812B (30 LED/m strip) Author: Scott C Created: 8th July 2015 Arduino IDE: 1.6.4 Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html Description: This sketch will display a heart beat on a 5m Neopixel LED strip. Requires a Grove Ear-clip heart rate sensor and a Neopixel strip. This project makes use of the FastLED library: http://fastled.io/ You may need to modify the code below to accomodate your specific LED strip. See the FastLED library site for more details. ================================================================================================== */ //This project needs the FastLED library - link in the description. #include "FastLED.h" //The total number of LEDs being used is 150 #define NUM_LEDS 150 // The data pin for the NeoPixel strip is connected to digital Pin 6 on the Arduino #define DATA_PIN 6 //Attach the Grove Ear-clip heart rate sensor to digital pin 2 on the Arduino. #define EAR_CLIP 2 //Initialise the LED array CRGB leds[NUM_LEDS]; //Initialise the global variables used to control the LED animation int ledNum = 0; //Keep track of the LEDs boolean beated = false; //Used to identify when the heart has beated int randomR = 0; //randomR used to randomise the fade-out of the LEDs //================================================================================================ // setup() : Is used to initialise the LED strip //================================================================================================ void setup() { FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS); //Set digital pin 2 (Ear-clip heart rate sensor) as an INPUT pinMode(EAR_CLIP, INPUT);} //================================================================================================ // loop() : Take readings from the Ear-clip sensor, and display the animation on the LED strip //================================================================================================ void loop() { //If the Ear-clip sensor moves from LOW to HIGH, call the beatTriggered method if(digitalRead(EAR_CLIP)>0){ //beatTriggered() is only called if the 'beated' variable is false. //This prevents multiple triggers from the same beat. ifbeated){ beatTriggered(); } } else { beated = false; //Change the 'beated' variable to false when the Ear-clip heart rate sensor is reading LOW. } //Fade the LEDs by 1 unit/cycle, when the heart is at 'rest' (i.e. between beats) fadeLEDs(5);} //================================================================================================ // beatTriggered() : This is the LED animation sequence when the heart beats //================================================================================================ void beatTriggered(){ //Ignite 30 LEDs with a red value between 0 to 255 for(int i = 0; i<30; i++){ //The red channel is randomised to a value between 0 to 255 leds[ledNum].r=random8(); FastLED.show(); //Call the fadeLEDs method after every 3rd LED is lit. if(ledNum%3==0){ fadeLEDs(5); } //Move to the next LED ledNum++; //Make sure to move back to the beginning if the animation falls off the end of the strip if(ledNum>(NUM_LEDS-1)){ ledNum=0; } } //Ignite 20 LEDS with a blue value between 0 to 120 for(int i = 0; i<20; i++){ //The blue channel is randomised to a value between 0 to 120 leds[ledNum].b=random8(120); FastLED.show(); //Call the fadeLEDs method after every 3rd LED is lit. if(ledNum%3==0){ fadeLEDs(5); } //Move to the next LED ledNum++; //Make sure to move back to the beginning if the animation falls off the end of the strip if(ledNum>(NUM_LEDS-1)){ ledNum=0; } } //Change the 'beated' variable to true, until the Ear-Clip sensor reads LOW. beated=true;} //================================================================================================ // fadeLEDs() : The fading effect of the LEDs when the Heart is resting (Ear-clip reads LOW) //================================================================================================ void fadeLEDs(int fadeVal){ for (int i = 0; i<NUM_LEDS; i++){ //Fade every LED by the fadeVal amount leds[i].fadeToBlackBy( fadeVal ); //Randomly re-fuel some of the LEDs that are currently lit (1% chance per cycle) //This enhances the twinkling effect. if(leds[i].r>10){ randomR = random8(100); if(randomR<1){ //Set the red channel to a value of 80 leds[i].r=80; //Increase the green channel to 20 - to add to the effect leds[i].g=20; } } } FastLED.show();}


 

NeoPixel Strip connection

The NeoPixel strip is rolled up when you first get it. You will notice that there are wires on both sides of the strip. This allows you to chain LED strips together to make longer strips. The more LEDs you have, the more current you will need. Connect your Arduino and power supply to the left side of the strip, with the arrows pointing to the right. (i.e. the side with the "female" jst connector).
 



NeoPixel Strip Wires

There are 5 wires that come pre-attached to either side of the LED strip.
 

 
You don't have to use ALL FIVE wires, however you will need at least one of each colour: red, white & green.
 

 

Fritzing sketch

The following diagram will show you how to wire everything together
 
(click to enlarge)

Arduino Power considerations

Please note that the Arduino is powered by a USB cable.
If you plan to power the Arduino from your power supply, you will need to disconnect the USB cable from the Arduino FIRST, then connect a wire from the 5V line on the Power supply to the VIN pin on the Arduino. Do NOT connect the USB cable to the Arduino while the VIN wire is connected.
 

 

Large Capacitor

Adafruit also recommend the use of a large capacitor across the + and - terminals of the LED strip to "prevent the initial onrush of current from damaging the pixels". Adafruit recommends a capacitor that is 1000uF, 6.3V or higher. I used a 4700uF 16V Electrolytic Capacitor.
 

 

Resistor on Data Pin

Another recommendation from Adafruit is to place a "300 to 500 Ohm resistor" between the Arduino's data pin and the data input on the first NeoPixel to prevent voltage spikes that can damage the first pixel. I used a 330 Ohm resistor.
 

 

Grove Ear-clip heart rate sensor connection

The Grove Base shield makes it easy to connect Grove modules to the Arduino. If you have a Grove Base shield, you will need to connect the Ear-clip heart rate sensor to Digital pin 2 as per the diagram below.
 

 

Completed construction

Once you have everything connected, you can plug the USB cable into the Arduino, and turn on the LED power supply. Attach the ear-clip to your ear (or to your finger) and allow a few seconds to allow the sensor to register your pulse. The LED strip will light up with every heart beat with an animation that moves from one end of the strip to the other in just three heart beats. When the ear-clip is not connected to your ear or finger, the LEDs should remain off. However, the ear clip may "trigger" a heart beat when opening or closing the clip.
 
Here is a picture of all the components (fully assembled).
 


Concluding comments


This very affordable LED strip allows you to create amazing animations over a greater distance. I thought that having less LEDs per metre would make the animations look "jittery", but I was wrong, they look amazing. One of the good things about this strip is the amount of space between each Neopixel, allowing you to easily cut and join the strip to the size and shape you need.
 
This LED strip is compatible with the FastLED library, which makes for easy LED animation programming. While I used this LED strip to display my heart beat, you could just as easily use it to display the output of any other sensor attached to the Arduino.
 



If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.


 
 
             

 
This project would not have been possible without OpenLab's collaborative effort.
Please visit their site for more cool projects.



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.