Posts with «serial» label

Arduino Heart Rate Monitor


Project Description


Heart Rate Monitors are very popular at the moment.
There is something very appealing about watching the pattern of your own heart beat. And once you see it, there is an unstoppable urge to try and control it. This simple project will allow you to visualize your heart beat, and will calculate your heart rate. Keep reading to learn how to create your very own heart rate monitor.


 

Parts Required:


Fritzing Sketch


 

 
 
 

Grove Base Shield to Module Connections


 


 

Arduino Sketch


 

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
/* =================================================================================================
      Project: Arduino Heart rate monitor
       Author: Scott C
      Created: 21st April 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This is a simple sketch that uses a Grove Ear-clip Heart Rate sensor attached to an Arduino UNO,
               which sends heart rate data to the computer via Serial communication. You can see the raw data
               using the Serial monitor on the Arduino IDE, however, this sketch was specifically
               designed to interface with the matching Processing sketch for a much nicer graphical display.
               NO LIBRARIES REQUIRED.
=================================================================================================== */

#define Heart 2                            //Attach the Grove Ear-clip sensor to digital pin 2.
#define LED 4                              //Attach an LED to digital pin 4

boolean beat = false; /* This "beat" variable is used to control the timing of the Serial communication
                                           so that data is only sent when there is a "change" in digital readings. */

//==SETUP==========================================================================================
void setup() {
  Serial.begin(9600); //Initialise serial communication
  pinMode(Heart, INPUT); //Set digital pin 2 (heart rate sensor pin) as an INPUT
  pinMode(LED, OUTPUT); //Set digital pin 4 (LED) to an OUTPUT
}


//==LOOP============================================================================================
void loop() {
  if(digitalRead(Heart)>0){ //The heart rate sensor will trigger HIGH when there is a heart beat
    if<!beat){><span>//Only send data when it first discovers a heart beat - otherwise it will send a high value multiple times</span><br />      beat=<span>true</span>; <span>//By changing the beat variable to true, it stops further transmissions of the high signal</span><br />      <span>digitalWrite</span>(LED, <span>HIGH</span>); <span>//Turn the LED on </span><br />      <span><b>Serial</b></span>.<span>println</span>(1023); <span>//Send the high value to the computer via Serial communication.</span><br />    }<br />  } <span>else</span> { <span>//If the reading is LOW, </span><br />    <span>if</span>(beat){ <span>//and if this has just changed from HIGH to LOW (first low reading)</span><br />      beat=<span>false</span>; <span>//change the beat variable to false (to stop multiple transmissions)</span><br />      <span>digitalWrite</span>(LED, <span>LOW</span>); <span>//Turn the LED off.</span><br />      <span><b>Serial</b></span>.<span>println</span>(0); <span>//then send a low value to the computer via Serial communication.</span><br />    }<br />  }<br />}</pre> </td> </tr> </table></div></p> <br />  <br />   <br />  <br />  <p> <h4><a href="https://processing.org/download/?processing">Processing Sketch</a></h4> <br />  <div> <table> <tr> <td> <pre> 1<br /> 2<br /> 3<br /> 4<br /> 5<br /> 6<br /> 7<br /> 8<br /> 9<br /> 10<br /> 11<br /> 12<br /> 13<br /> 14<br /> 15<br /> 16<br /> 17<br /> 18<br /> 19<br /> 20<br /> 21<br /> 22<br /> 23<br /> 24<br /> 25<br /> 26<br /> 27<br /> 28<br /> 29<br /> 30<br /> 31<br /> 32<br /> 33<br /> 34<br /> 35<br /> 36<br /> 37<br /> 38<br /> 39<br /> 40<br /> 41<br /> 42<br /> 43<br /> 44<br /> 45<br /> 46<br /> 47<br /> 48<br /> 49<br /> 50<br /> 51<br /> 52<br /> 53<br /> 54<br /> 55<br /> 56<br /> 57<br /> 58<br /> 59<br /> 60<br /> 61<br /> 62<br /> 63<br /> 64<br /> 65<br /> 66<br /> 67<br /> 68<br /> 69<br /> 70<br /> 71<br /> 72<br /> 73<br /> 74<br /> 75<br /> 76<br /> 77<br /> 78<br /> 79<br /> 80<br /> 81<br /> 82<br /> 83<br /> 84<br /> 85<br /> 86<br /> 87<br /> 88<br /> 89<br /> 90<br /> 91<br /> 92<br /> 93<br /> 94<br /> 95<br /> 96<br /> 97<br /> 98<br /> 99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br /></pre> </td> <td> <pre><br /><span>/* =================================================================================================</span><br /><span>       Project: Arduino Heart rate monitor</span><br /><span>        Author: Scott C</span><br /><span>       Created: 21st April 2015</span><br /><span>Processing IDE: 2.2.1</span><br /><span>       Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html</span><br /><span>   Description: A Grove Ear-clip heart rate sensor allows an Arduino UNO to sense your pulse.</span><br /><span>                The data obtained by the Arduino can then be sent to the computer via Serial communication</span><br /><span>                which is then displayed graphically using this Processing sketch.</span><br /><span>                </span><br /><span>=================================================================================================== */</span><br /><br /><span>import</span> processing.serial.*; <span>// Import the serial library to allow Serial communication with the Arduino</span><br /><br /><span>int</span> numOfRecs = 45; <span>// numOfRecs: The number of rectangles to display across the screen</span><br />Rectangle[] myRecs = <span>new</span> Rectangle[numOfRecs]; <span>// myRecs[]: Is the array of Rectangles. Rectangle is a custom class (programmed within this sketch)</span><br /><br />Serial myPort;                                         <br /><span>String</span> comPortString=<span>"0"</span>; <span>//comPortString: Is used to hold the string received from the Arduino</span><br /><span>float</span> arduinoValue = 0; <span>//arduinoValue: Is the float variable converted from comPortString</span><br /><span>boolean</span> beat = <span>false</span>; <span>// beat: Used to control for multiple high/low signals coming from the Arduino</span><br /><br /><span>int</span> totalTime = 0; <span>// totalTime: Is the variable used to identify the total time between beats</span><br /><span>int</span> lastTime = 0; <span>// lastTime: Is the variable used to remember when the last beat took place</span><br /><span>int</span> beatCounter = 0; <span>// beatCounter: Is used to keep track of the number of beats (in order to calculate the average BPM)</span><br /><span>int</span> totalBeats = 10; <span>// totalBeats: Tells the computer that we want to calculate the average BPM using 10 beats.</span><br /><span>int</span>[] BPM = <span>new</span> <span>int</span>[totalBeats]; <span>// BPM[]: Is the Beat Per Minute (BPM) array - to hold 10 BPM calculations</span><br /><span>int</span> sumBPM = 0; <span>// sumBPM: Is used to sum the BPM[] array values, and is then used to calculate the average BPM.</span><br /><span>int</span> avgBPM = 0; <span>// avgBPM: Is the variable used to hold the average BPM calculated value.</span><br /><br /><span>PFont</span> f, f2; <span>// f & f2 : Are font related variables. Used to store font properties. </span><br /><br /><br /><span>//==SETUP==============================================================================================</span><br /><span>void</span> <span><b>setup</b></span>(){<br />  <span>size</span>(<span>displayWidth</span>,<span>displayHeight</span>); <span>// Set the size of the display to match the monitor width and height</span><br />  <span>smooth</span>(); <span>// Draw all shapes with smooth edges.</span><br />  f = <span>createFont</span>(<span>"Arial"</span>,24); <span>// Initialise the "f" font variable - used for the "calibrating" text displayed at the beginning</span><br />  f2 = <span>createFont</span>(<span>"Arial"</span>,96); <span>// Initialise the "f2" font variable - used for the avgBPM display on screen</span><br />  <br />  <span>for</span>(<span>int</span> i=0; i<numOfRecs; i++){ <span>// Initialise the array of rectangles</span><br />    myRecs[i] = <span>new</span> Rectangle(i, numOfRecs);<br />  }<br />  <br />  <span>for</span>(<span>int</span> i=0; i<totalBeats; i++){ <span>// Initialise the BPM array</span><br />    BPM[i] = 0;<br />  }<br />  <br />  myPort = <span>new</span> Serial(<span>this</span>, Serial.<span>list</span>()[0], 9600); <span>// Start Serial communication with the Arduino using a baud rate of 9600</span><br />  myPort.bufferUntil(<span>'\n'</span>); <span>// Trigger a SerialEvent on new line</span><br />}<br /><br /><br /><span>//==DRAW==============================================================================================</span><br /><span>void</span> <span><b>draw</b></span>(){<br />  <span>background</span>(0); <span>// Set the background to BLACK (this clears the screen each time)</span><br />  drawRecs();                                           <span>// Method call to draw the rectangles on the screen</span><br />  drawBPM();                                            <span>// Method call to draw the avgBPM value to the top right of the screen</span><br />}<br /><br /><br /><span>//==drawRecs==========================================================================================</span><br /><span>void</span> drawRecs(){ <span>// This custom method will draw the rectangles on the screen </span><br />  myRecs[0].setSize(arduinoValue);                      <span>// Set the first rectangle to match arduinoValue; any positive value will start the animation.</span><br />  <span>for</span>(<span>int</span> i=numOfRecs-1; i>0; i--){ <span>// The loop counts backwards for coding efficiency - and is used to draw all of the rectangles to screen</span><br />    myRecs[i].setMult(i);                               <span>// setMulti creates the specific curve pattern. </span><br />    myRecs[i].setRed(avgBPM);                           <span>// The rectangles become more "Red" with higher avgBPM values</span><br />    myRecs[i].setSize(myRecs[i-1].getH());              <span>// The current rectangle size is determined by the height of the rectangle immediately to it's left</span><br />    <span>fill</span>(myRecs[i].getR(),myRecs[i].getG(), myRecs[i].getB()); <span>// Set the colour of this rectangle</span><br />    <span>rect</span>(myRecs[i].getX(), myRecs[i].getY(), myRecs[i].getW(), myRecs[i].getH()); <span>// Draw this rectangle</span><br />  }<br />}<br /><br /><br /><span>//==drawBPM===========================================================================================</span><br /><span>void</span> drawBPM(){ <span>// This custom method is used to calculate the avgBPM and draw it to screen.</span><br />  sumBPM = 0;                                           <span>// Reset the sumBPM variable</span><br />  avgBPM = 0;                                           <span>// Reset the avgBPM variable</span><br />  <span>boolean</span> calibrating = <span>false</span>; <span>// calibrating: this boolean variable is used to control when the avgBPM is displayed to screen</span><br />  <br />  <span>for</span>(<span>int</span> i=1; i<totalBeats; i++){<br />    sumBPM = sumBPM + BPM[i-1];                         <span>// Sum all of the BPM values in the BPM array.</span><br />    <span>if</span>(BPM[i-1]<1){ <span>// If any BPM values are equal to 0, then set the calibrating variable to true. </span><br />      calibrating = <span>true</span>; <span>// This will be used later to display "calibrating" on the screen.</span><br />    }<br />  }<br />  avgBPM = sumBPM/(totalBeats-1);                       <span>// Calculate the average BPM from all BPM values</span><br />                                                        <br />  <span>fill</span>(255); <span>// The text will be displayed as WHITE text</span><br />  <span>if</span>(calibrating){<br />    <span>textFont</span>(f);<br />    <span>text</span>(<span>"Calibrating"</span>, (4*<span>width</span>)/5, (<span>height</span>/5)); <span>// If the calibrating variable is TRUE, then display the word "Calibrating" on screen</span><br />    <span>fill</span>(0); <span>// Change the fill and stroke to black (0) so that other text is "hidden" while calibrating variable is TRUE</span><br />    <span>stroke</span>(0);<br />  } <span>else</span> {<br />    <span>textFont</span>(f2);<br />    <span>text</span>(avgBPM, (4*<span>width</span>)/5, (<span>height</span>/5)); <span>// If the calibrating variable is FALSE, then display the avgBPM variable on screen</span><br />    <span>stroke</span>(255); <span>// Change the stroke to white (255) to show the white line underlying the word BPM.</span><br />  }<br />  <br />   <span>textFont</span>(f);<br />   <span>text</span>(<span>"BPM"</span>, (82*<span>width</span>)/100, (<span>height</span>/11)); <span>// This will display the underlined word "BPM" when calibrating variable is FALSE.</span><br />   <span>line</span>((80*<span>width</span>)/100, (<span>height</span>/10),(88*<span>width</span>)/100, (<span>height</span>/10));<br />   <span>stroke</span>(0);<br />}<br /><br /><br /><span>//==serialEvent===========================================================================================</span><br /><span>void</span> serialEvent(Serial cPort){ <span>// This will be triggered every time a "new line" of data is received from the Arduino</span><br /> comPortString = cPort.readStringUntil(<span>'\n'</span>); <span>// Read this data into the comPortString variable.</span><br /> <span>if</span>(comPortString != <span>null</span>) { <span>// If the comPortString variable is not NULL then</span><br />   comPortString=<span>trim</span>(comPortString); <span>// trim any white space around the text.</span><br />   <span>int</span> i = <span>int</span>(<span>map</span>(<span>Integer</span>.<span>parseInt</span>(comPortString),1,1023,1,<span>height</span>)); <span>// convert the string to an integer, and map the value so that the rectangle will fit within the screen.</span><br />   arduinoValue = <span>float</span>(i); <span>// Convert the integer into a float value.</span><br />   <span>if</span> (!beat){<br />     <span>if</span>(arduinoValue>0){ <span>// When a beat is detected, the "trigger" method is called.</span><br />       trigger(<span>millis</span>()); <span>// millis() creates a timeStamp of when the beat occured.</span><br />       beat=<span>true</span>; <span>// The beat variable is changed to TRUE to register that a beat has been detected.</span><br />     }<br />   }<br />   <span>if</span> (arduinoValue<1){ <span>// When the Arduino value returns back to zero, we will need to change the beat status to FALSE.</span><br />     beat = <span>false</span>;<br />   }<br /> }<br />} <br /><br /><br /><span>//==trigger===========================================================================================</span><br /><span>void</span> trigger(<span>int</span> time){ <span>// This method is used to calculate the Beats per Minute (BPM) and to store the last 10 BPMs into the BPM[] array.</span><br />  totalTime = time - lastTime;                         <span>// totalTime = the current beat time minus the last time there was a beat.</span><br />  lastTime = time;                                     <span>// Set the lastTime variable to the current "time" for the next round of calculations.</span><br />  BPM[beatCounter] = 60000/totalTime;                  <span>// Calculate BPM from the totalTime. 60000 = 1 minute.</span><br />  beatCounter++;                                       <span>// Increment the beatCounter </span><br />  <span>if</span> (beatCounter>totalBeats-1){ <span>// Reset the beatCounter when the total number of BPMs have been stored into the BPM[] array.</span><br />    beatCounter=0;                                     <span>// This allows us to keep the last 10 BPM calculations at all times.</span><br />  }<br />}<br /><br /><br /><span>//==sketchFullScreen==========================================================================================</span><br /><span>boolean</span> sketchFullScreen() { <span>// This puts Processing into Full Screen Mode</span><br /> <span>return</span> <span>true</span>;<br />}<br /><br /><br /><span>//==Rectangle CLASS==================================================================================*********</span><br /><span>class</span> Rectangle{<br />  <span>float</span> xPos, defaultY, yPos, myWidth, myHeight, myMultiplier; <span>// Variables used for drawing rectangles</span><br />  <span>int</span> blueVal, greenVal, redVal; <span>// Variables used for the rectangle colour</span><br />  <br />  Rectangle(<span>int</span> recNum, <span>int</span> nRecs){ <span>// The rectangles are constructed using two variables. The total number of rectangles to be displayed, and the identification of this rectangle (recNum)</span><br />    myWidth = <span>displayWidth</span>/nRecs; <span>// The width of the rectangle is determined by the screen width and the total number of rectangles.</span><br />    xPos = recNum * myWidth;                                      <span>// The x Position of this rectangle is determined by the width of the rectangles (all same) and the rectangle identifier.</span><br />    defaultY=<span>displayHeight</span>/2; <span>// The default Y position of the rectangle is half way down the screen.</span><br />    yPos = defaultY;                                              <span>// yPos is used to adjust the position of the rectangle as the size changes.</span><br />    myHeight = 1;                                                 <span>// The height of the rectangle starts at 1 pixel</span><br />    myMultiplier = 1;                                             <span>// The myMultiplier variable will be used to create the funnel shaped path for the rectangles.</span><br />    redVal = 0;                                                   <span>// The red Value starts off being 0 - but changes with avgBPM. Higher avgBPM means higher redVal</span><br />    <br />    <span>if</span> (recNum>0){ <span>// The blue Value progressively increases with every rectangle (moving to the right of the screen)</span><br />      blueVal = (recNum*255)/nRecs;<br />    } <span>else</span> {<br />      blueVal = 0;<br />    }<br />    greenVal = 255-blueVal;                                       <span>// Initially, the green value is at the opposite end of the spectrum to the blue value.</span><br />  }<br />  <br />  <span>void</span> setSize(<span>float</span> newSize){ <span>// This is used to set the new size of each rectangle </span><br />    myHeight=newSize*myMultiplier;<br />    yPos=defaultY-(newSize/2);<br />  }<br />  <br />  <span>void</span> setMult(<span>int</span> i){ <span>// The multiplier is a function of COS, which means that it varies from 1 to 0.</span><br />    myMultiplier = <span>cos</span>(<span>radians</span>(i)); <span>// You can try other functions to experience different effects.</span><br />  }<br />  <br />  <span>void</span> setRed(<span>int</span> r){<br />    redVal = <span>int</span>(<span>constrain</span>(<span>map</span>(<span>float</span>(r), 60, 100, 0, 255),0,255)); <span>// setRed is used to change the redValue based on the "normal" value for resting BPM (60-100). </span><br />    greenVal = 255 - redVal;                                       <span>// When the avgBPM > 100, redVal will equal 255, and the greenVal will equal 0.</span><br />  }                                                                <span>// When the avgBPM < 60, redVal will equal 0, and greenVal will equal 255.</span><br />  <br />  <span>float</span> getX(){ <span>// get the x Position of the rectangle</span><br />    <span>return</span> xPos;<br />  }<br /> <br />  <span>float</span> getY(){ <span>// get the y Position of the rectangle</span><br />    <span>return</span> yPos;<br />  }<br />  <br />  <span>float</span> getW(){ <span>// get the width of the rectangle</span><br />    <span>return</span> myWidth;<br />  }<br />  <br />  <span>float</span> getH(){ <span>// get the height of the rectangle</span><br />    <span>return</span> myHeight;<br />  }<br />  <br />  <span>float</span> getM(){ <span>// get the Multiplier of the rectangle</span><br />    <span>return</span> myMultiplier;<br />  }<br />  <br />  <span>int</span> getB(){ <span>// get the "blue" component of the rectangle colour</span><br />    <span>return</span> blueVal;<br />  }<br />  <br />  <span>int</span> getR(){ <span>// get the "red" component of the rectangle colour</span><br />    <span>return</span> redVal;<br />  }<br />  <br />  <span>int</span> getG(){ <span>// get the "green" component of the rectangle colour</span><br />    <span>return</span> greenVal;<br />  }<br />}<br /><br /></pre> </td> </tr> </table></div></p> <br />  <br /> <p> <h4>Processing Code Discussion:</h4><br /> </p><p> The Rectangle class was created to store relevant information about each rectangle. By using a custom class, we were able to design our rectangles any way we wanted. These rectangles have properties and methods which allow us to easily control their position, size and colour. By adding some smart functionality to each rectangle, we were able to get the rectangle to automatically position and colour itself based on key values. </p> <p> The Serial library is used to allow communication with the Arduino. In this Processing sketch, the values obtained from the Arduino were converted to floats to allow easy calulations of the beats per minute (BPM). I am aware that I have over-engineered the serialEvent method somewhat, because the Arduino is only really sending two values. I didn't really need to convert the String. But I am happy with the end result, and it does the job I needed it to... </p> <div> <p> <div> <a href="http://4.bp.blogspot.com/-EVTCQ3vkgGc/VTnOarlOWSI/AAAAAAAABdc/MslEU5oirAY/s1600/Complete%2BWorkstation2.jpg"><img src="http://4.bp.blogspot.com/-EVTCQ3vkgGc/VTnOarlOWSI/AAAAAAAABdc/MslEU5oirAY/s1600/Complete%2BWorkstation2.jpg" /> </a> </div> </p> </div> </div><!--separator --><img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /><p> <div> This project is quite simple. I designed it so that you could omit the Processing code if you wanted to. In that scenario, you would only be left with a blinking LED that blinks in time with your pulse. The Processing code takes this project to the next level. It provides a nice animation and calculates the beats per minute (BPM). <br />   <br /> I hope you liked this tutorial. Please feel free to share it, comment or give it a plus one. If you didn't like it, I would still appreciate your constructive feedback. </div> <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> </p><p> <div> If you like this page, please do me a favour and show your appreciation : <br /> <br />  <br /> Visit my <a href="https://plus.google.com/u/0/b/107402020974762902161/107402020974762902161/posts">ArduinoBasics Google + page</a>.<br /> Follow me on Twitter by looking for <a href="https://twitter.com/ArduinoBasics">ScottC @ArduinoBasics</a>.<br /> I can also be found on <a href="https://www.pinterest.com/ArduinoBasics/">Pinterest</a> and <a href="https://instagram.com/arduinobasics">Instagram</a>. <br /> Have a look at my videos on my <a href="https://www.youtube.com/user/ScottCMe/videos">YouTube channel</a>.<br /> </div> </p> <br />  <br />  <p> <div> <a href="http://3.bp.blogspot.com/-x_TA-qhOCzM/VTnULXoWhQI/AAAAAAAABds/quh02BWGsec/s1600/Slide1.JPG"><img src="http://3.bp.blogspot.com/-x_TA-qhOCzM/VTnULXoWhQI/AAAAAAAABds/quh02BWGsec/s1600/Slide1.JPG" /></a></div><br /> </p> <br />  <br />  <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> <p> However, if you do not have a google profile... <br />Feel free to share this page with your friends in any way you see fit. </p>

Arduino Heart Rate Monitor


Project Description


Heart Rate Monitors are very popular at the moment.
There is something very appealing about watching the pattern of your own heart beat. And once you see it, there is an unstoppable urge to try and control it. This simple project will allow you to visualize your heart beat, and will calculate your heart rate. Keep reading to learn how to create your very own heart rate monitor.


 

Parts Required:


Fritzing Sketch


 

 
 
 

Grove Base Shield to Module Connections


 


 

Arduino Sketch


 

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
/* =================================================================================================
      Project: Arduino Heart rate monitor
       Author: Scott C
      Created: 21st April 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This is a simple sketch that uses a Grove Ear-clip Heart Rate sensor attached to an Arduino UNO,
               which sends heart rate data to the computer via Serial communication. You can see the raw data
               using the Serial monitor on the Arduino IDE, however, this sketch was specifically
               designed to interface with the matching Processing sketch for a much nicer graphical display.
               NO LIBRARIES REQUIRED.
=================================================================================================== */

#define Heart 2                            //Attach the Grove Ear-clip sensor to digital pin 2.
#define LED 4                              //Attach an LED to digital pin 4

boolean beat = false; /* This "beat" variable is used to control the timing of the Serial communication
                                           so that data is only sent when there is a "change" in digital readings. */

//==SETUP==========================================================================================
void setup() {
  Serial.begin(9600); //Initialise serial communication
  pinMode(Heart, INPUT); //Set digital pin 2 (heart rate sensor pin) as an INPUT
  pinMode(LED, OUTPUT); //Set digital pin 4 (LED) to an OUTPUT
}


//==LOOP============================================================================================
void loop() {
  if(digitalRead(Heart)>0){ //The heart rate sensor will trigger HIGH when there is a heart beat
    if<!beat){><span>//Only send data when it first discovers a heart beat - otherwise it will send a high value multiple times</span><br />      beat=<span>true</span>; <span>//By changing the beat variable to true, it stops further transmissions of the high signal</span><br />      <span>digitalWrite</span>(LED, <span>HIGH</span>); <span>//Turn the LED on </span><br />      <span><b>Serial</b></span>.<span>println</span>(1023); <span>//Send the high value to the computer via Serial communication.</span><br />    }<br />  } <span>else</span> { <span>//If the reading is LOW, </span><br />    <span>if</span>(beat){ <span>//and if this has just changed from HIGH to LOW (first low reading)</span><br />      beat=<span>false</span>; <span>//change the beat variable to false (to stop multiple transmissions)</span><br />      <span>digitalWrite</span>(LED, <span>LOW</span>); <span>//Turn the LED off.</span><br />      <span><b>Serial</b></span>.<span>println</span>(0); <span>//then send a low value to the computer via Serial communication.</span><br />    }<br />  }<br />}</pre> </td> </tr> </table></div></p> <br />  <br />   <br />  <br />  <p> <h4><a href="https://processing.org/download/?processing">Processing Sketch</a></h4> <br />  <div> <table> <tr> <td> <pre> 1<br /> 2<br /> 3<br /> 4<br /> 5<br /> 6<br /> 7<br /> 8<br /> 9<br /> 10<br /> 11<br /> 12<br /> 13<br /> 14<br /> 15<br /> 16<br /> 17<br /> 18<br /> 19<br /> 20<br /> 21<br /> 22<br /> 23<br /> 24<br /> 25<br /> 26<br /> 27<br /> 28<br /> 29<br /> 30<br /> 31<br /> 32<br /> 33<br /> 34<br /> 35<br /> 36<br /> 37<br /> 38<br /> 39<br /> 40<br /> 41<br /> 42<br /> 43<br /> 44<br /> 45<br /> 46<br /> 47<br /> 48<br /> 49<br /> 50<br /> 51<br /> 52<br /> 53<br /> 54<br /> 55<br /> 56<br /> 57<br /> 58<br /> 59<br /> 60<br /> 61<br /> 62<br /> 63<br /> 64<br /> 65<br /> 66<br /> 67<br /> 68<br /> 69<br /> 70<br /> 71<br /> 72<br /> 73<br /> 74<br /> 75<br /> 76<br /> 77<br /> 78<br /> 79<br /> 80<br /> 81<br /> 82<br /> 83<br /> 84<br /> 85<br /> 86<br /> 87<br /> 88<br /> 89<br /> 90<br /> 91<br /> 92<br /> 93<br /> 94<br /> 95<br /> 96<br /> 97<br /> 98<br /> 99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br /></pre> </td> <td> <pre><br /><span>/* =================================================================================================</span><br /><span>       Project: Arduino Heart rate monitor</span><br /><span>        Author: Scott C</span><br /><span>       Created: 21st April 2015</span><br /><span>Processing IDE: 2.2.1</span><br /><span>       Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html</span><br /><span>   Description: A Grove Ear-clip heart rate sensor allows an Arduino UNO to sense your pulse.</span><br /><span>                The data obtained by the Arduino can then be sent to the computer via Serial communication</span><br /><span>                which is then displayed graphically using this Processing sketch.</span><br /><span>                </span><br /><span>=================================================================================================== */</span><br /><br /><span>import</span> processing.serial.*; <span>// Import the serial library to allow Serial communication with the Arduino</span><br /><br /><span>int</span> numOfRecs = 45; <span>// numOfRecs: The number of rectangles to display across the screen</span><br />Rectangle[] myRecs = <span>new</span> Rectangle[numOfRecs]; <span>// myRecs[]: Is the array of Rectangles. Rectangle is a custom class (programmed within this sketch)</span><br /><br />Serial myPort;                                         <br /><span>String</span> comPortString=<span>"0"</span>; <span>//comPortString: Is used to hold the string received from the Arduino</span><br /><span>float</span> arduinoValue = 0; <span>//arduinoValue: Is the float variable converted from comPortString</span><br /><span>boolean</span> beat = <span>false</span>; <span>// beat: Used to control for multiple high/low signals coming from the Arduino</span><br /><br /><span>int</span> totalTime = 0; <span>// totalTime: Is the variable used to identify the total time between beats</span><br /><span>int</span> lastTime = 0; <span>// lastTime: Is the variable used to remember when the last beat took place</span><br /><span>int</span> beatCounter = 0; <span>// beatCounter: Is used to keep track of the number of beats (in order to calculate the average BPM)</span><br /><span>int</span> totalBeats = 10; <span>// totalBeats: Tells the computer that we want to calculate the average BPM using 10 beats.</span><br /><span>int</span>[] BPM = <span>new</span> <span>int</span>[totalBeats]; <span>// BPM[]: Is the Beat Per Minute (BPM) array - to hold 10 BPM calculations</span><br /><span>int</span> sumBPM = 0; <span>// sumBPM: Is used to sum the BPM[] array values, and is then used to calculate the average BPM.</span><br /><span>int</span> avgBPM = 0; <span>// avgBPM: Is the variable used to hold the average BPM calculated value.</span><br /><br /><span>PFont</span> f, f2; <span>// f & f2 : Are font related variables. Used to store font properties. </span><br /><br /><br /><span>//==SETUP==============================================================================================</span><br /><span>void</span> <span><b>setup</b></span>(){<br />  <span>size</span>(<span>displayWidth</span>,<span>displayHeight</span>); <span>// Set the size of the display to match the monitor width and height</span><br />  <span>smooth</span>(); <span>// Draw all shapes with smooth edges.</span><br />  f = <span>createFont</span>(<span>"Arial"</span>,24); <span>// Initialise the "f" font variable - used for the "calibrating" text displayed at the beginning</span><br />  f2 = <span>createFont</span>(<span>"Arial"</span>,96); <span>// Initialise the "f2" font variable - used for the avgBPM display on screen</span><br />  <br />  <span>for</span>(<span>int</span> i=0; i<numOfRecs; i++){ <span>// Initialise the array of rectangles</span><br />    myRecs[i] = <span>new</span> Rectangle(i, numOfRecs);<br />  }<br />  <br />  <span>for</span>(<span>int</span> i=0; i<totalBeats; i++){ <span>// Initialise the BPM array</span><br />    BPM[i] = 0;<br />  }<br />  <br />  myPort = <span>new</span> Serial(<span>this</span>, Serial.<span>list</span>()[0], 9600); <span>// Start Serial communication with the Arduino using a baud rate of 9600</span><br />  myPort.bufferUntil(<span>'\n'</span>); <span>// Trigger a SerialEvent on new line</span><br />}<br /><br /><br /><span>//==DRAW==============================================================================================</span><br /><span>void</span> <span><b>draw</b></span>(){<br />  <span>background</span>(0); <span>// Set the background to BLACK (this clears the screen each time)</span><br />  drawRecs();                                           <span>// Method call to draw the rectangles on the screen</span><br />  drawBPM();                                            <span>// Method call to draw the avgBPM value to the top right of the screen</span><br />}<br /><br /><br /><span>//==drawRecs==========================================================================================</span><br /><span>void</span> drawRecs(){ <span>// This custom method will draw the rectangles on the screen </span><br />  myRecs[0].setSize(arduinoValue);                      <span>// Set the first rectangle to match arduinoValue; any positive value will start the animation.</span><br />  <span>for</span>(<span>int</span> i=numOfRecs-1; i>0; i--){ <span>// The loop counts backwards for coding efficiency - and is used to draw all of the rectangles to screen</span><br />    myRecs[i].setMult(i);                               <span>// setMulti creates the specific curve pattern. </span><br />    myRecs[i].setRed(avgBPM);                           <span>// The rectangles become more "Red" with higher avgBPM values</span><br />    myRecs[i].setSize(myRecs[i-1].getH());              <span>// The current rectangle size is determined by the height of the rectangle immediately to it's left</span><br />    <span>fill</span>(myRecs[i].getR(),myRecs[i].getG(), myRecs[i].getB()); <span>// Set the colour of this rectangle</span><br />    <span>rect</span>(myRecs[i].getX(), myRecs[i].getY(), myRecs[i].getW(), myRecs[i].getH()); <span>// Draw this rectangle</span><br />  }<br />}<br /><br /><br /><span>//==drawBPM===========================================================================================</span><br /><span>void</span> drawBPM(){ <span>// This custom method is used to calculate the avgBPM and draw it to screen.</span><br />  sumBPM = 0;                                           <span>// Reset the sumBPM variable</span><br />  avgBPM = 0;                                           <span>// Reset the avgBPM variable</span><br />  <span>boolean</span> calibrating = <span>false</span>; <span>// calibrating: this boolean variable is used to control when the avgBPM is displayed to screen</span><br />  <br />  <span>for</span>(<span>int</span> i=1; i<totalBeats; i++){<br />    sumBPM = sumBPM + BPM[i-1];                         <span>// Sum all of the BPM values in the BPM array.</span><br />    <span>if</span>(BPM[i-1]<1){ <span>// If any BPM values are equal to 0, then set the calibrating variable to true. </span><br />      calibrating = <span>true</span>; <span>// This will be used later to display "calibrating" on the screen.</span><br />    }<br />  }<br />  avgBPM = sumBPM/(totalBeats-1);                       <span>// Calculate the average BPM from all BPM values</span><br />                                                        <br />  <span>fill</span>(255); <span>// The text will be displayed as WHITE text</span><br />  <span>if</span>(calibrating){<br />    <span>textFont</span>(f);<br />    <span>text</span>(<span>"Calibrating"</span>, (4*<span>width</span>)/5, (<span>height</span>/5)); <span>// If the calibrating variable is TRUE, then display the word "Calibrating" on screen</span><br />    <span>fill</span>(0); <span>// Change the fill and stroke to black (0) so that other text is "hidden" while calibrating variable is TRUE</span><br />    <span>stroke</span>(0);<br />  } <span>else</span> {<br />    <span>textFont</span>(f2);<br />    <span>text</span>(avgBPM, (4*<span>width</span>)/5, (<span>height</span>/5)); <span>// If the calibrating variable is FALSE, then display the avgBPM variable on screen</span><br />    <span>stroke</span>(255); <span>// Change the stroke to white (255) to show the white line underlying the word BPM.</span><br />  }<br />  <br />   <span>textFont</span>(f);<br />   <span>text</span>(<span>"BPM"</span>, (82*<span>width</span>)/100, (<span>height</span>/11)); <span>// This will display the underlined word "BPM" when calibrating variable is FALSE.</span><br />   <span>line</span>((80*<span>width</span>)/100, (<span>height</span>/10),(88*<span>width</span>)/100, (<span>height</span>/10));<br />   <span>stroke</span>(0);<br />}<br /><br /><br /><span>//==serialEvent===========================================================================================</span><br /><span>void</span> serialEvent(Serial cPort){ <span>// This will be triggered every time a "new line" of data is received from the Arduino</span><br /> comPortString = cPort.readStringUntil(<span>'\n'</span>); <span>// Read this data into the comPortString variable.</span><br /> <span>if</span>(comPortString != <span>null</span>) { <span>// If the comPortString variable is not NULL then</span><br />   comPortString=<span>trim</span>(comPortString); <span>// trim any white space around the text.</span><br />   <span>int</span> i = <span>int</span>(<span>map</span>(<span>Integer</span>.<span>parseInt</span>(comPortString),1,1023,1,<span>height</span>)); <span>// convert the string to an integer, and map the value so that the rectangle will fit within the screen.</span><br />   arduinoValue = <span>float</span>(i); <span>// Convert the integer into a float value.</span><br />   <span>if</span> (!beat){<br />     <span>if</span>(arduinoValue>0){ <span>// When a beat is detected, the "trigger" method is called.</span><br />       trigger(<span>millis</span>()); <span>// millis() creates a timeStamp of when the beat occured.</span><br />       beat=<span>true</span>; <span>// The beat variable is changed to TRUE to register that a beat has been detected.</span><br />     }<br />   }<br />   <span>if</span> (arduinoValue<1){ <span>// When the Arduino value returns back to zero, we will need to change the beat status to FALSE.</span><br />     beat = <span>false</span>;<br />   }<br /> }<br />} <br /><br /><br /><span>//==trigger===========================================================================================</span><br /><span>void</span> trigger(<span>int</span> time){ <span>// This method is used to calculate the Beats per Minute (BPM) and to store the last 10 BPMs into the BPM[] array.</span><br />  totalTime = time - lastTime;                         <span>// totalTime = the current beat time minus the last time there was a beat.</span><br />  lastTime = time;                                     <span>// Set the lastTime variable to the current "time" for the next round of calculations.</span><br />  BPM[beatCounter] = 60000/totalTime;                  <span>// Calculate BPM from the totalTime. 60000 = 1 minute.</span><br />  beatCounter++;                                       <span>// Increment the beatCounter </span><br />  <span>if</span> (beatCounter>totalBeats-1){ <span>// Reset the beatCounter when the total number of BPMs have been stored into the BPM[] array.</span><br />    beatCounter=0;                                     <span>// This allows us to keep the last 10 BPM calculations at all times.</span><br />  }<br />}<br /><br /><br /><span>//==sketchFullScreen==========================================================================================</span><br /><span>boolean</span> sketchFullScreen() { <span>// This puts Processing into Full Screen Mode</span><br /> <span>return</span> <span>true</span>;<br />}<br /><br /><br /><span>//==Rectangle CLASS==================================================================================*********</span><br /><span>class</span> Rectangle{<br />  <span>float</span> xPos, defaultY, yPos, myWidth, myHeight, myMultiplier; <span>// Variables used for drawing rectangles</span><br />  <span>int</span> blueVal, greenVal, redVal; <span>// Variables used for the rectangle colour</span><br />  <br />  Rectangle(<span>int</span> recNum, <span>int</span> nRecs){ <span>// The rectangles are constructed using two variables. The total number of rectangles to be displayed, and the identification of this rectangle (recNum)</span><br />    myWidth = <span>displayWidth</span>/nRecs; <span>// The width of the rectangle is determined by the screen width and the total number of rectangles.</span><br />    xPos = recNum * myWidth;                                      <span>// The x Position of this rectangle is determined by the width of the rectangles (all same) and the rectangle identifier.</span><br />    defaultY=<span>displayHeight</span>/2; <span>// The default Y position of the rectangle is half way down the screen.</span><br />    yPos = defaultY;                                              <span>// yPos is used to adjust the position of the rectangle as the size changes.</span><br />    myHeight = 1;                                                 <span>// The height of the rectangle starts at 1 pixel</span><br />    myMultiplier = 1;                                             <span>// The myMultiplier variable will be used to create the funnel shaped path for the rectangles.</span><br />    redVal = 0;                                                   <span>// The red Value starts off being 0 - but changes with avgBPM. Higher avgBPM means higher redVal</span><br />    <br />    <span>if</span> (recNum>0){ <span>// The blue Value progressively increases with every rectangle (moving to the right of the screen)</span><br />      blueVal = (recNum*255)/nRecs;<br />    } <span>else</span> {<br />      blueVal = 0;<br />    }<br />    greenVal = 255-blueVal;                                       <span>// Initially, the green value is at the opposite end of the spectrum to the blue value.</span><br />  }<br />  <br />  <span>void</span> setSize(<span>float</span> newSize){ <span>// This is used to set the new size of each rectangle </span><br />    myHeight=newSize*myMultiplier;<br />    yPos=defaultY-(newSize/2);<br />  }<br />  <br />  <span>void</span> setMult(<span>int</span> i){ <span>// The multiplier is a function of COS, which means that it varies from 1 to 0.</span><br />    myMultiplier = <span>cos</span>(<span>radians</span>(i)); <span>// You can try other functions to experience different effects.</span><br />  }<br />  <br />  <span>void</span> setRed(<span>int</span> r){<br />    redVal = <span>int</span>(<span>constrain</span>(<span>map</span>(<span>float</span>(r), 60, 100, 0, 255),0,255)); <span>// setRed is used to change the redValue based on the "normal" value for resting BPM (60-100). </span><br />    greenVal = 255 - redVal;                                       <span>// When the avgBPM > 100, redVal will equal 255, and the greenVal will equal 0.</span><br />  }                                                                <span>// When the avgBPM < 60, redVal will equal 0, and greenVal will equal 255.</span><br />  <br />  <span>float</span> getX(){ <span>// get the x Position of the rectangle</span><br />    <span>return</span> xPos;<br />  }<br /> <br />  <span>float</span> getY(){ <span>// get the y Position of the rectangle</span><br />    <span>return</span> yPos;<br />  }<br />  <br />  <span>float</span> getW(){ <span>// get the width of the rectangle</span><br />    <span>return</span> myWidth;<br />  }<br />  <br />  <span>float</span> getH(){ <span>// get the height of the rectangle</span><br />    <span>return</span> myHeight;<br />  }<br />  <br />  <span>float</span> getM(){ <span>// get the Multiplier of the rectangle</span><br />    <span>return</span> myMultiplier;<br />  }<br />  <br />  <span>int</span> getB(){ <span>// get the "blue" component of the rectangle colour</span><br />    <span>return</span> blueVal;<br />  }<br />  <br />  <span>int</span> getR(){ <span>// get the "red" component of the rectangle colour</span><br />    <span>return</span> redVal;<br />  }<br />  <br />  <span>int</span> getG(){ <span>// get the "green" component of the rectangle colour</span><br />    <span>return</span> greenVal;<br />  }<br />}<br /><br /></pre> </td> </tr> </table></div></p> <br />  <br /> <p> <h4>Processing Code Discussion:</h4><br /> </p><p> The Rectangle class was created to store relevant information about each rectangle. By using a custom class, we were able to design our rectangles any way we wanted. These rectangles have properties and methods which allow us to easily control their position, size and colour. By adding some smart functionality to each rectangle, we were able to get the rectangle to automatically position and colour itself based on key values. </p> <p> The Serial library is used to allow communication with the Arduino. In this Processing sketch, the values obtained from the Arduino were converted to floats to allow easy calulations of the beats per minute (BPM). I am aware that I have over-engineered the serialEvent method somewhat, because the Arduino is only really sending two values. I didn't really need to convert the String. But I am happy with the end result, and it does the job I needed it to... </p> <div> <p> <div> <a href="http://4.bp.blogspot.com/-EVTCQ3vkgGc/VTnOarlOWSI/AAAAAAAABdc/MslEU5oirAY/s1600/Complete%2BWorkstation2.jpg"><img src="http://4.bp.blogspot.com/-EVTCQ3vkgGc/VTnOarlOWSI/AAAAAAAABdc/MslEU5oirAY/s1600/Complete%2BWorkstation2.jpg" /> </a> </div> </p> </div> </div><!--separator --><img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /><p> <div> This project is quite simple. I designed it so that you could omit the Processing code if you wanted to. In that scenario, you would only be left with a blinking LED that blinks in time with your pulse. The Processing code takes this project to the next level. It provides a nice animation and calculates the beats per minute (BPM). <br />   <br /> I hope you liked this tutorial. Please feel free to share it, comment or give it a plus one. If you didn't like it, I would still appreciate your constructive feedback. </div> <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> </p><p> <div> If you like this page, please do me a favour and show your appreciation : <br /> <br />  <br /> Visit my <a href="https://plus.google.com/u/0/b/107402020974762902161/107402020974762902161/posts">ArduinoBasics Google + page</a>.<br /> Follow me on Twitter by looking for <a href="https://twitter.com/ArduinoBasics">ScottC @ArduinoBasics</a>.<br /> I can also be found on <a href="https://www.pinterest.com/ArduinoBasics/">Pinterest</a> and <a href="https://instagram.com/arduinobasics">Instagram</a>. <br /> Have a look at my videos on my <a href="https://www.youtube.com/user/ScottCMe/videos">YouTube channel</a>.<br /> </div> </p> <br />  <br />  <p> <div> <a href="http://3.bp.blogspot.com/-x_TA-qhOCzM/VTnULXoWhQI/AAAAAAAABds/quh02BWGsec/s1600/Slide1.JPG"><img src="http://3.bp.blogspot.com/-x_TA-qhOCzM/VTnULXoWhQI/AAAAAAAABds/quh02BWGsec/s1600/Slide1.JPG" /></a></div><br /> </p> <br />  <br />  <br />  <div> <p> <!--separator --> <img src="https://images-blogger-opensocial.googleusercontent.com/gadgets/proxy?url=http%3A%2F%2F1.bp.blogspot.com%2F-XQiwNpdqOxk%2FT_rKCzDh4nI%2FAAAAAAAAAQY%2FOfYBljhU6Lk%2Fs1600%2FSeparator.jpg&container=blogger&gadget=a&rewriteMime=image%2F*" /><br /> <br /> </p> </div> <p> However, if you do not have a google profile... <br />Feel free to share this page with your friends in any way you see fit. </p>

Arduino BeatBox

Create your very own Arduino BeatBox !

Home-made capacitive touch sensors are used to trigger the MP3 drum sounds stored on the Grove Serial MP3 player. I have used a number of tricks to get the most out of this module, and I was quite impressed on how well it did. Over 130 sounds were loaded onto the SDHC card. Most were drum sounds, but I added some farm animal noises to provide an extra element of surprise and entertainment. You can put any sounds you want on the module and play them back quickly. We'll put the Grove Serial MP3 module through it's paces and make it into a neat little BeatBox !!


Key learning objectives

  • How to make your own beatbox
  • How to make capacitive drum pad sensors without using resistors
  • How to speed up Arduino's Analog readings for better performance
  • How to generate random numbers on your Arduino


Parts Required:

Making the drum pads


 
 

Fritzing Sketch


 


 
 

Grove Connections


 


 
 

Grove Connections (without base shield)


 


 
 

Arduino Sketch


 
  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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

/* =================================================================================================
      Project: Arduino Beatbox
       Author: Scott C
      Created: 9th April 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This project uses home made capacitive sensors to trigger over 130 MP3 sounds
               on the Grove Serial MP3 player. 
               
               The ADCTouch library is used to eliminate the resistors from the Capacitive sensing circuit. 
               The code used for capacitive sensing was adapted from the ADCTouch library example sketches. 
               You can find the ADCTouch library and relevant example code here:
               http://playground.arduino.cc/Code/ADCTouch
               
               "Advanced Arduino ADC" is used to improve the analogRead() speed, and enhance the
               drum pad or capacitive sensor response time. The Advanced Arduino ADC code 
               was adapted from this site:
               http://www.microsmart.co.za/technical/2014/03/01/advanced-arduino-adc/
               
               
=================================================================================================== */
  #include <ADCTouch.h>
  #include <SoftwareSerial.h>
  
  
  //Global variables
  //===================================================================================================
  int potPin = A4; //Grove Sliding potentiometer is connected to Analog Pin 4
  int potVal = 0;
  byte mp3Vol = 0; //Variable used to control the volume of the MP3 player
  byte oldVol = 0;
  
  int buttonPin = 5; //Grove Button is connected to Digital Pin 5
  int buttonStatus = 0;
  
  byte SongNum[4] = {0x01,0x02,0x03,0x04}; //The first 4 songs will be assigned to the drum pads upon initialisation
  byte numOfSongs = 130; //Total number of MP3 songs/sounds loaded onto the SDHC card
  
  long randNumber; //Variable used to hold the random number - used to randomise the sounds.
  
  int ledState[4]; //Used to keep track of the status of all LEDs (on or off)
  int counter = 0;
  
  SoftwareSerial mp3(3, 4); // The Grove MP3 Player is connected to Arduino digital Pin 3 and 4 (Serial communication)
       
  int ref0, ref1, ref2, ref3; //reference values to remove offset
  int threshold = 100;
      
  // Define the ADC prescalers
  const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
  const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  
  
  
  //Setup()
  //===================================================================================================
  void setup(){
    //Initialise the Grove MP3 Module
    delay(2500); //Allow the MP3 module to power up
    mp3.begin(9600); //Begin Serial communication with the MP3 module
    setPlayMode(0x00);                        //0x00 = Single song - played once ie. not repeated. (default)
    
    //Define the Grove Button as an INPUT
    pinMode(buttonPin, INPUT);
    
    //Define the 4 LED Pins as OUTPUTs
    pinMode(8, OUTPUT); //Green LED
    pinMode(9, OUTPUT); //Blue LED
    pinMode(10, OUTPUT); //Red LED
    pinMode(11, OUTPUT); //Yellow LED
    
    //Make sure each LED is OFF, and store the state of the LED into a variable.
    for(int i=8;i<12;i++){
      digitalWrite(i, LOW);
      ledState[i-8]=0;
    } 
    
    //Double our clock speed from 125 kHz to 250 kHz
    ADCSRA &= ~PS_128;   // set up the ADC
    ADCSRA |= PS_64;    // set our own prescaler to 64
    
    //Create reference values to account for the capacitance of each pad.
    ref0 = ADCTouch.read(A0, 500);
    ref1 = ADCTouch.read(A1, 500); //Take 500 readings
    ref2 = ADCTouch.read(A2, 500);
    ref3 = ADCTouch.read(A3, 500);
    
     //This helps to randomise the drum pads.
     randomSeed(analogRead(0));
  }
  
  
  
  // Loop()
  //===================================================================================================
  void loop(){
     
    //Take a reading from the Grove Sliding Potentiometer, and set volume accordingly
    potVal = analogRead(potPin);
    mp3Vol = map(potVal, 0, 1023, 0,31); // Convert the potentometer reading (0 - 1023) to fit within the MP3 player's Volume range (0 - 31)
    if((mp3Vol>(oldVol+1))|(mp3Vol<(oldVol-1))){ // Only make a change to the Volume on the Grove MP3 player when the potentiometer value changes
      oldVol = mp3Vol;
      setVolume(mp3Vol);
      delay(10); // This delay is necessary with Serial communication to MP3 player
    }
    
    //Take a reading from the Pin attached to the Grove Button. If pressed, randomise the MP3 songs/sounds for each drum pad, and make the LEDs blink randomly.
    buttonStatus = digitalRead(buttonPin);
    if(buttonStatus==HIGH){
      SongNum[0]=randomSongChooser(1, 30);
      SongNum[1]=randomSongChooser(31, 60);
      SongNum[2]=randomSongChooser(61, 86);
      SongNum[3]=randomSongChooser(87, (int)numOfSongs);
      randomLEDBlink();
    }
    
    //Get the capacitive readings from each drum pad: 50 readings are taken from each pad. (default is 100)
    int value0 = ADCTouch.read(A0,50); // Green drum pad
    int value1 = ADCTouch.read(A1,50); // Blue drum pad
    int value2 = ADCTouch.read(A2,50); // Red drum pad
    int value3 = ADCTouch.read(A3,50); // Yellow drum pad
    
    //Remove the offset to account for the baseline capacitance of each pad.
    value0 -= ref0;       
    value1 -= ref1;
    value2 -= ref2;
    value3 -= ref3;
    
    
    //If any of the values exceed the designated threshold, then play the song/sound associated with that drum pad.
    //The associated LED will stay on for the whole time the drum pad is pressed, providing the value remains above the threshold.
    //The LED will turn off when the pad is not being touched or pressed.
    if(value0>threshold){
      digitalWrite(8, HIGH);
      playSong(00,SongNum[0]);
    }else{
      digitalWrite(8,LOW);
    }
    
    if(value1>threshold){
      digitalWrite(9, HIGH);
      playSong(00,SongNum[1]);
    }else{
      digitalWrite(9,LOW);
    }
    
    if(value2>threshold){
      digitalWrite(10, HIGH);
      playSong(00,SongNum[2]);
    }else{
      digitalWrite(10,LOW);
    }
    
    if(value3>threshold){
      digitalWrite(11, HIGH);
      playSong(00,SongNum[3]);
    }else{
      digitalWrite(11,LOW);
    }
  }
      
   
  // writeToMP3:
  // a generic function that simplifies each of the methods used to control the Grove MP3 Player
  //===================================================================================================
  void writeToMP3(byte MsgLEN, byte A, byte B, byte C, byte D, byte E, byte F){
    byte codeMsg[] = {MsgLEN, A,B,C,D,E,F};
    mp3.write(0x7E); //Start Code for every command = 0x7E
    for(byte i = 0; i<MsgLEN+1; i++){
      mp3.write(codeMsg[i]); //Send the rest of the command to the GROVE MP3 player
    }
  }
  
  
  //setPlayMode: defines how each song is to be played
  //===================================================================================================
  void setPlayMode(byte playMode){
    /* playMode options:
          0x00 = Single song - played only once ie. not repeated.  (default)
          0x01 = Single song - cycled ie. repeats over and over.
          0x02 = All songs - cycled 
          0x03 = play songs randomly                                           */
    writeToMP3(0x03, 0xA9, playMode, 0x7E, 0x00, 0x00, 0x00);  
  }
  
  
  //playSong: tells the Grove MP3 player to play the song/sound, and also which song/sound to play
  //===================================================================================================
  void playSong(byte songHbyte, byte songLbyte){
    writeToMP3(0x04, 0xA0, songHbyte, songLbyte, 0x7E, 0x00, 0x00);            
    delay(100);
  }
  
  
  //setVolume: changes the Grove MP3 player's volume to the designated level (0 to 31)
  //===================================================================================================
  void setVolume(byte Volume){
    byte tempVol = constrain(Volume, 0, 31); //Volume range = 00 (muted) to 31 (max volume)
    writeToMP3(0x03, 0xA7, tempVol, 0x7E, 0x00, 0x00, 0x00); 
  }
  
  
  //randomSongChooser: chooses a random song to play. The range of songs to choose from
  //is limited and defined by the startSong and endSong parameters.
  //===================================================================================================
  byte randomSongChooser(int startSong, int endSong){
    randNumber = random(startSong, endSong);
    return((byte) randNumber);
  }
  
  
  //randomLEDBlink: makes each LED blink randomly. The LEDs are attached to digital pins 8 to 12.
  //===================================================================================================
  void randomLEDBlink(){
   counter=8;
   for(int i=0; i<40; i++){
     int x = constrain((int)random(8,12),8,12);
     toggleLED(x);
     delay(random(50,100-i));
   }
     
    for(int i=8;i<12;i++){
      digitalWrite(i, HIGH);
    }
    delay(1000);
    for(int i=8;i<12;i++){
      digitalWrite(i, LOW);
      ledState[i-8]=0;
    }
  }
  
  
  //toggleLED: is used by the randomLEDBlink method to turn each LED on and off (randomly).
  //===================================================================================================
  void toggleLED(int pinNum){
    ledState[pinNum-8]= !ledState[pinNum-8];
    digitalWrite(pinNum, ledState[pinNum-8]);
  }


 

Arduino Code Discussion

You can see from the Arduino code above, that it uses the ADCTouch library. This library was chosen over the Capacitive Sensing Library to eliminate the need for a high value resistor which are commonly found in Capacitive Sensing projects).
 
To increase the speed of the Analog readings, I utilised one of the "Advanced Arduino ADC" techniques described by Guy van den Berg on this Microsmart website.
 
The readings are increased by modifying the Arduino's ADC clock speed from 125kHz to 250 kHz. I did notice an overall better response time with this modification. However, the Grove Serial MP3 player is limited by it's inability to play more than one song or sound at a time. This means that if you hit another drum pad while the current sound is playing, it will stop playing the current sound, and then play the selected sound. The speed at which it can perform this task was quite impressive. In fact it was much better than I thought it would be. But if you are looking for polyphonic playability, you will be dissapointed.
 
This Serial MP3 module makes use of a high quality MP3 audio chip known as the "WT5001". Therefore, you should be able to get some additional features and functionality from this document. Plus you may find some extra useful info from the Seeedstudio wiki. I have re-used some code from the Arduino Boombox tutorial... you will find extra Grove Serial MP3 functions on that page.
 
I will warn you... the Grove Serial MP3 player can play WAV files, however for some reason it would not play many of the sound files in this format. Once the sounds were converted to the MP3 format, I did not look back. So if you decide to take on this project, make sure your sound files are in MP3 format, you'll have a much better outcome.
 
I decided to introduce a random sound selection for each drum pad to extend the novelty of this instrument, which meant that I had to come up with a fancy way to illuminate the LEDs. I demonstrated some of my other LED sequences on my instagram account. I sometimes use instagram to show my work in progress.
 
Have a look at the video below to see this project in action, and putting the Grove Serial MP3 player through it's paces.
 

The Video


 


First there was the Arduino Boombox, and now we have the Arduino Beatbox..... who knows what will come next !
 
Whenever I create a new project, I like to improve my Arduino knowledge. Sometimes it takes me into some rather complicated topics. There is a lot I do not know about Arduino, but I am enjoying the journey. I hope you are too !! Please Google plus one this post if it helped you in any way. These tutorials are free, which means I survive on feedback and plus ones... all you have to do is just scroll a little bit more and click that button :)

 
 



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.

Arduino BeatBox

Create your very own Arduino BeatBox !

Home-made capacitive touch sensors are used to trigger the MP3 drum sounds stored on the Grove Serial MP3 player. I have used a number of tricks to get the most out of this module, and I was quite impressed on how well it did. Over 130 sounds were loaded onto the SDHC card. Most were drum sounds, but I added some farm animal noises to provide an extra element of surprise and entertainment. You can put any sounds you want on the module and play them back quickly. We'll put the Grove Serial MP3 module through it's paces and make it into a neat little BeatBox !!


Key learning objectives

  • How to make your own beatbox
  • How to make capacitive drum pad sensors without using resistors
  • How to speed up Arduino's Analog readings for better performance
  • How to generate random numbers on your Arduino


Parts Required:

Making the drum pads


 
 

Fritzing Sketch


 


 
 

Grove Connections


 


 
 

Grove Connections (without base shield)


 


 
 

Arduino Sketch


 
  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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

/* =================================================================================================
      Project: Arduino Beatbox
       Author: Scott C
      Created: 9th April 2015
  Arduino IDE: 1.6.2
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html
  Description: This project uses home made capacitive sensors to trigger over 130 MP3 sounds
               on the Grove Serial MP3 player. 
               
               The ADCTouch library is used to eliminate the resistors from the Capacitive sensing circuit. 
               The code used for capacitive sensing was adapted from the ADCTouch library example sketches. 
               You can find the ADCTouch library and relevant example code here:
               http://playground.arduino.cc/Code/ADCTouch
               
               "Advanced Arduino ADC" is used to improve the analogRead() speed, and enhance the
               drum pad or capacitive sensor response time. The Advanced Arduino ADC code 
               was adapted from this site:
               http://www.microsmart.co.za/technical/2014/03/01/advanced-arduino-adc/
               
               
=================================================================================================== */
  #include <ADCTouch.h>
  #include <SoftwareSerial.h>
  
  
  //Global variables
  //===================================================================================================
  int potPin = A4; //Grove Sliding potentiometer is connected to Analog Pin 4
  int potVal = 0;
  byte mp3Vol = 0; //Variable used to control the volume of the MP3 player
  byte oldVol = 0;
  
  int buttonPin = 5; //Grove Button is connected to Digital Pin 5
  int buttonStatus = 0;
  
  byte SongNum[4] = {0x01,0x02,0x03,0x04}; //The first 4 songs will be assigned to the drum pads upon initialisation
  byte numOfSongs = 130; //Total number of MP3 songs/sounds loaded onto the SDHC card
  
  long randNumber; //Variable used to hold the random number - used to randomise the sounds.
  
  int ledState[4]; //Used to keep track of the status of all LEDs (on or off)
  int counter = 0;
  
  SoftwareSerial mp3(3, 4); // The Grove MP3 Player is connected to Arduino digital Pin 3 and 4 (Serial communication)
       
  int ref0, ref1, ref2, ref3; //reference values to remove offset
  int threshold = 100;
      
  // Define the ADC prescalers
  const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1);
  const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  
  
  
  //Setup()
  //===================================================================================================
  void setup(){
    //Initialise the Grove MP3 Module
    delay(2500); //Allow the MP3 module to power up
    mp3.begin(9600); //Begin Serial communication with the MP3 module
    setPlayMode(0x00);                        //0x00 = Single song - played once ie. not repeated. (default)
    
    //Define the Grove Button as an INPUT
    pinMode(buttonPin, INPUT);
    
    //Define the 4 LED Pins as OUTPUTs
    pinMode(8, OUTPUT); //Green LED
    pinMode(9, OUTPUT); //Blue LED
    pinMode(10, OUTPUT); //Red LED
    pinMode(11, OUTPUT); //Yellow LED
    
    //Make sure each LED is OFF, and store the state of the LED into a variable.
    for(int i=8;i<12;i++){
      digitalWrite(i, LOW);
      ledState[i-8]=0;
    } 
    
    //Double our clock speed from 125 kHz to 250 kHz
    ADCSRA &= ~PS_128;   // set up the ADC
    ADCSRA |= PS_64;    // set our own prescaler to 64
    
    //Create reference values to account for the capacitance of each pad.
    ref0 = ADCTouch.read(A0, 500);
    ref1 = ADCTouch.read(A1, 500); //Take 500 readings
    ref2 = ADCTouch.read(A2, 500);
    ref3 = ADCTouch.read(A3, 500);
    
     //This helps to randomise the drum pads.
     randomSeed(analogRead(0));
  }
  
  
  
  // Loop()
  //===================================================================================================
  void loop(){
     
    //Take a reading from the Grove Sliding Potentiometer, and set volume accordingly
    potVal = analogRead(potPin);
    mp3Vol = map(potVal, 0, 1023, 0,31); // Convert the potentometer reading (0 - 1023) to fit within the MP3 player's Volume range (0 - 31)
    if((mp3Vol>(oldVol+1))|(mp3Vol<(oldVol-1))){ // Only make a change to the Volume on the Grove MP3 player when the potentiometer value changes
      oldVol = mp3Vol;
      setVolume(mp3Vol);
      delay(10); // This delay is necessary with Serial communication to MP3 player
    }
    
    //Take a reading from the Pin attached to the Grove Button. If pressed, randomise the MP3 songs/sounds for each drum pad, and make the LEDs blink randomly.
    buttonStatus = digitalRead(buttonPin);
    if(buttonStatus==HIGH){
      SongNum[0]=randomSongChooser(1, 30);
      SongNum[1]=randomSongChooser(31, 60);
      SongNum[2]=randomSongChooser(61, 86);
      SongNum[3]=randomSongChooser(87, (int)numOfSongs);
      randomLEDBlink();
    }
    
    //Get the capacitive readings from each drum pad: 50 readings are taken from each pad. (default is 100)
    int value0 = ADCTouch.read(A0,50); // Green drum pad
    int value1 = ADCTouch.read(A1,50); // Blue drum pad
    int value2 = ADCTouch.read(A2,50); // Red drum pad
    int value3 = ADCTouch.read(A3,50); // Yellow drum pad
    
    //Remove the offset to account for the baseline capacitance of each pad.
    value0 -= ref0;       
    value1 -= ref1;
    value2 -= ref2;
    value3 -= ref3;
    
    
    //If any of the values exceed the designated threshold, then play the song/sound associated with that drum pad.
    //The associated LED will stay on for the whole time the drum pad is pressed, providing the value remains above the threshold.
    //The LED will turn off when the pad is not being touched or pressed.
    if(value0>threshold){
      digitalWrite(8, HIGH);
      playSong(00,SongNum[0]);
    }else{
      digitalWrite(8,LOW);
    }
    
    if(value1>threshold){
      digitalWrite(9, HIGH);
      playSong(00,SongNum[1]);
    }else{
      digitalWrite(9,LOW);
    }
    
    if(value2>threshold){
      digitalWrite(10, HIGH);
      playSong(00,SongNum[2]);
    }else{
      digitalWrite(10,LOW);
    }
    
    if(value3>threshold){
      digitalWrite(11, HIGH);
      playSong(00,SongNum[3]);
    }else{
      digitalWrite(11,LOW);
    }
  }
      
   
  // writeToMP3:
  // a generic function that simplifies each of the methods used to control the Grove MP3 Player
  //===================================================================================================
  void writeToMP3(byte MsgLEN, byte A, byte B, byte C, byte D, byte E, byte F){
    byte codeMsg[] = {MsgLEN, A,B,C,D,E,F};
    mp3.write(0x7E); //Start Code for every command = 0x7E
    for(byte i = 0; i<MsgLEN+1; i++){
      mp3.write(codeMsg[i]); //Send the rest of the command to the GROVE MP3 player
    }
  }
  
  
  //setPlayMode: defines how each song is to be played
  //===================================================================================================
  void setPlayMode(byte playMode){
    /* playMode options:
          0x00 = Single song - played only once ie. not repeated.  (default)
          0x01 = Single song - cycled ie. repeats over and over.
          0x02 = All songs - cycled 
          0x03 = play songs randomly                                           */
    writeToMP3(0x03, 0xA9, playMode, 0x7E, 0x00, 0x00, 0x00);  
  }
  
  
  //playSong: tells the Grove MP3 player to play the song/sound, and also which song/sound to play
  //===================================================================================================
  void playSong(byte songHbyte, byte songLbyte){
    writeToMP3(0x04, 0xA0, songHbyte, songLbyte, 0x7E, 0x00, 0x00);            
    delay(100);
  }
  
  
  //setVolume: changes the Grove MP3 player's volume to the designated level (0 to 31)
  //===================================================================================================
  void setVolume(byte Volume){
    byte tempVol = constrain(Volume, 0, 31); //Volume range = 00 (muted) to 31 (max volume)
    writeToMP3(0x03, 0xA7, tempVol, 0x7E, 0x00, 0x00, 0x00); 
  }
  
  
  //randomSongChooser: chooses a random song to play. The range of songs to choose from
  //is limited and defined by the startSong and endSong parameters.
  //===================================================================================================
  byte randomSongChooser(int startSong, int endSong){
    randNumber = random(startSong, endSong);
    return((byte) randNumber);
  }
  
  
  //randomLEDBlink: makes each LED blink randomly. The LEDs are attached to digital pins 8 to 12.
  //===================================================================================================
  void randomLEDBlink(){
   counter=8;
   for(int i=0; i<40; i++){
     int x = constrain((int)random(8,12),8,12);
     toggleLED(x);
     delay(random(50,100-i));
   }
     
    for(int i=8;i<12;i++){
      digitalWrite(i, HIGH);
    }
    delay(1000);
    for(int i=8;i<12;i++){
      digitalWrite(i, LOW);
      ledState[i-8]=0;
    }
  }
  
  
  //toggleLED: is used by the randomLEDBlink method to turn each LED on and off (randomly).
  //===================================================================================================
  void toggleLED(int pinNum){
    ledState[pinNum-8]= !ledState[pinNum-8];
    digitalWrite(pinNum, ledState[pinNum-8]);
  }


 

Arduino Code Discussion

You can see from the Arduino code above, that it uses the ADCTouch library. This library was chosen over the Capacitive Sensing Library to eliminate the need for a high value resistor which are commonly found in Capacitive Sensing projects).
 
To increase the speed of the Analog readings, I utilised one of the "Advanced Arduino ADC" techniques described by Guy van den Berg on this Microsmart website.
 
The readings are increased by modifying the Arduino's ADC clock speed from 125kHz to 250 kHz. I did notice an overall better response time with this modification. However, the Grove Serial MP3 player is limited by it's inability to play more than one song or sound at a time. This means that if you hit another drum pad while the current sound is playing, it will stop playing the current sound, and then play the selected sound. The speed at which it can perform this task was quite impressive. In fact it was much better than I thought it would be. But if you are looking for polyphonic playability, you will be dissapointed.
 
This Serial MP3 module makes use of a high quality MP3 audio chip known as the "WT5001". Therefore, you should be able to get some additional features and functionality from this document. Plus you may find some extra useful info from the Seeedstudio wiki. I have re-used some code from the Arduino Boombox tutorial... you will find extra Grove Serial MP3 functions on that page.
 
I will warn you... the Grove Serial MP3 player can play WAV files, however for some reason it would not play many of the sound files in this format. Once the sounds were converted to the MP3 format, I did not look back. So if you decide to take on this project, make sure your sound files are in MP3 format, you'll have a much better outcome.
 
I decided to introduce a random sound selection for each drum pad to extend the novelty of this instrument, which meant that I had to come up with a fancy way to illuminate the LEDs. I demonstrated some of my other LED sequences on my instagram account. I sometimes use instagram to show my work in progress.
 
Have a look at the video below to see this project in action, and putting the Grove Serial MP3 player through it's paces.
 

The Video


 


First there was the Arduino Boombox, and now we have the Arduino Beatbox..... who knows what will come next !
 
Whenever I create a new project, I like to improve my Arduino knowledge. Sometimes it takes me into some rather complicated topics. There is a lot I do not know about Arduino, but I am enjoying the journey. I hope you are too !! Please Google plus one this post if it helped you in any way. These tutorials are free, which means I survive on feedback and plus ones... all you have to do is just scroll a little bit more and click that button :)

 
 



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.

Arduino Boombox

Add sound or music to your project using the "Grove Serial MP3 Player".

An Arduino UNO will be used to control the Grove Serial MP3 player by sending it specific serial commands. The Grove Base Shield allows for the easy connection of Grove sensor modules to an Arduino UNO without the need for a breadboard. A sliding potentiometer, switch and button will be connected to the Base shield along with the Serial MP3 player. A specific function will be assigned to each of the connected sensor modules to provide a useful interface:

  • Sliding Potentiometer – Volume control
  • Button – Next Song
  • Switch – On/Off (toggle)
Once the MP3 module is working the way we want, we can then build a simple enclosure for it. Grab a shoe-box, print out your favourite design, and make your very own Arduino BOOMBOX!


 

Video

Watch the following video to see the project in action
 


 
 

Parts Required:

Optional components (for the BoomBox Enclosure):
  • Empty Shoe Box
  • Paper
  • Printer
  • Glue
If I had a 3D printer - I would have printed my own enclosure, but a shoebox seems to work just fine.


 

Putting it Together

Place the Grove Base shield onto the Arduino UNO,
and then connect each of the Grove Modules as per the table below.
 


 

If you do not have a Grove Base shield,
you can still connect the modules directly to the Arduino as per the table below:
 


 

When you are finished connecting the modules, it should look something like this:
  (ignore the battery pack):
 

As you can see from the picture above. You can cut holes out of the shoebox and stick the modules in place. Please ignore the battery pack, because you won't use it until after you have uploaded the Arduino code.


 
 

Arduino Sketch


 
  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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193


/* ===============================================================================
      Project: Grove Serial MP3 Player overview
       Author: Scott C
      Created: 9th March 2015
  Arduino IDE: 1.6.0
      Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html

  Description: The following Arduino sketch will allow you to control a Grove Serial MP3 player
               with a Grove Sliding Potentiometer (volume), a Grove button (next song), 
               and a Grove Switch (on/off). It will also show you how to retrieve some useful information from the player. 
               Some functions are not used in this sketch,but have been included for your benefit. 
               
               Additional features and functionality can be found on the WT5001 voice chip datasheet 
               which I retrieved from here: http://goo.gl/ai6oQ9
               
               The Seeedstudio wiki was a very useful resource for getting started with the various Grove modules:
               http://goo.gl/xOiSCl
=============================================================================== */

#include <SoftwareSerial.h>
SoftwareSerial mp3(2, 3); // The Grove MP3 Player is connected to Arduino digital Pin 2 and 3 (Serial communication)
int potPin = A0; // The Sliding Potentiometer is connected to AnalogPin 0
int potVal = 0; // This is used to hold the value of the Sliding Potentiometer
byte mp3Vol = 0; // mp3Vol is used to calculate the Current volume of the Grove MP3 player
byte oldVol = 0; // oldVol is used to remember the previous volume level
int ledPin = A1; // The Grove sliding potentiometer has an onboard LED attached to Analog pin 1.

int switchPin = 12; // The Grove Switch(P) is connected to digital Pin 12
int switchStatus = 0; // This is used to hold the status of the switch
int switchChangeStatus = 0; // Used to identify when the switch status has changed

int buttonPin = 5; // The Grove Button is connected to digital pin 5
int buttonStatus = 0; // This is used to hold the status of the button



void setup(){
  //Initialise the Grove MP3 Module
  delay(2500);
  mp3.begin(9600);
  
        
  // initialize the pushbutton and switch pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(switchPin, INPUT);
  
  // set ledPin on the sliding potentiometer to OUTPUT
  pinMode(ledPin, OUTPUT);
  
  //You can view the following demostration output in the Serial Monitor
  demonstrate_GET_FUNCTIONS();     
}


void loop(){
  switchStatus = digitalRead(switchPin);
  if(switchStatus==HIGH){
    if(switchChangeStatus==LOW){ // When Arduino detects a change in the switchStatus (from LOW to HIGH) - play song
      setPlayMode(0x02);                     // Automatically cycle to the next song when the current song ends
      playSong(00,01);                       // Play the 1st song when you switch it on
      switchChangeStatus=HIGH;
    }
    
    potVal = analogRead(potPin); // Analog read values from the sliding potentiometer range from 0 to 1023
    analogWrite(ledPin, potVal/4); // Analog write values range from 0 to 255, and will turn LED ON once potentiometer reaches about half way (or more).
    mp3Vol = map(potVal, 0, 1023, 0,31); // Convert the potentometer reading (0 - 1023) to fit within the MP3 player's Volume range (0 - 31)
    if((mp3Vol>(oldVol+1))|(mp3Vol<(oldVol-1))){ // Only make a change to the Volume on the Grove MP3 player when the potentiometer value changes
      oldVol = mp3Vol;
      setVolume(mp3Vol);
      delay(10); // This delay is necessary with Serial communication to MP3 player
    }

    buttonStatus = digitalRead(buttonPin);
    if(buttonStatus==HIGH){ // When a button press is detected - play the next song
      playNextSong();
      delay(200); // This delay aims to prevent a "skipped" song due to slow button presses - can modify to suit.
    }
  } else {
    if(switchChangeStatus==HIGH){ // When switchStatus changes from HIGH to LOW - stop Song.
      stopSong();
      switchChangeStatus=LOW;
    }
  } 
}


// demonstrate_GET_FUNCTIONS  will show you how to retrieve some useful information from the Grove MP3 Player (using the Serial Monitor).
void demonstrate_GET_FUNCTIONS(){
        Serial.begin(9600);
        Serial.print("Volume: ");
        Serial.println(getVolume());
        Serial.print("Playing State: ");
        Serial.println(getPlayingState());
        Serial.print("# of Files in SD Card:");
        Serial.println(getNumberOfFiles());
        Serial.println("------------------------------");
}


// writeToMP3: is a generic function that aims to simplify all of the methods that control the Grove MP3 Player

void writeToMP3(byte MsgLEN, byte A, byte B, byte C, byte D, byte E, byte F){
  byte codeMsg[] = {MsgLEN, A,B,C,D,E,F};
  mp3.write(0x7E); //Start Code for every command = 0x7E
  for(byte i = 0; i<MsgLEN+1; i++){
    mp3.write(codeMsg[i]); //Send the rest of the command to the GROVE MP3 player
  }
}


/* The Following functions control the Grove MP3 Player : see datasheet for additional functions--------------------------------------------*/

void setPlayMode(byte playMode){
  /* playMode options:
        0x00 = Single song - played only once ie. not repeated.  (default)
        0x01 = Single song - cycled ie. repeats over and over.
        0x02 = All songs - cycled 
        0x03 = play songs randomly                                           */
        
  writeToMP3(0x03, 0xA9, playMode, 0x7E, 0x00, 0x00, 0x00);  
}


void playSong(byte songHbyte, byte songLbyte){ // Plays the selected song
  writeToMP3(0x04, 0xA0, songHbyte, songLbyte, 0x7E, 0x00, 0x00);            
}


void pauseSong(){ // Pauses the current song
  writeToMP3(0x02, 0xA3, 0x7E, 0x00, 0x00, 0x00, 0x00);
}


void stopSong(){ // Stops the current song
  writeToMP3(0x02, 0xA4, 0x7E, 0x00, 0x00, 0x00, 0x00);
}


void playNextSong(){ // Play the next song
  writeToMP3(0x02, 0xA5, 0x7E, 0x00, 0x00, 0x00, 0x00);
}


void playPreviousSong(){ // Play the previous song
  writeToMP3(0x02, 0xA6, 0x7E, 0x00, 0x00, 0x00, 0x00);
}


void addSongToPlayList(byte songHbyte, byte songLbyte){
  //Repeat this function for every song you wish to stack onto the playlist (max = 10 songs)
  writeToMP3(0x04, 0xA8, songHbyte, songLbyte, 0x7E, 0x00, 0x00);
}


void setVolume(byte Volume){ // Set the volume
  byte tempVol = constrain(Volume, 0, 31);
  //Volume range = 00 (muted) to 31 (max volume)
  writeToMP3(0x03, 0xA7, tempVol, 0x7E, 0x00, 0x00, 0x00); 
}



/* The following functions retrieve information from the Grove MP3 player : see data sheet for additional functions--------------*/

// getData: is a generic function to simplifly the other functions for retieving information from the Grove Serial MP3 player
byte getData(byte queryVal, int dataPosition){
  byte returnVal = 0x00;
  writeToMP3(0x02, queryVal, 0x7E, 0x00, 0x00, 0x00, 0x00);
  delay(50);
  for(int x = 0; x<dataPosition; x++){
    if(mp3.available()){
      returnVal = mp3.read();
      delay(50);
    }
  }
  return(returnVal);
}

byte getVolume(){ //Get the volume of the Grove Serial MP3 player
  //returns value from 0 - 31
  return(getData(0xC1, 4));
}

byte getPlayingState(){ //Get the playing state : Play / Stopped / Paused
  //returns 1: Play, 2: Stop, 3:Paused
  return(getData(0xC2, 2));
}


byte getNumberOfFiles(){ //Find out how many songs are on the SD card
  //returns the number of MP3 files on SD card
  return(getData(0xC4, 3));
}

You will notice from the code, that I did not utilise every function. I decided to include them for your benifit. This Serial MP3 module makes use of a high quality MP3 audio chip known as the "WT5001". Therefore, you should be able to get some additional features and functionality from this document. Plus you may find some extra useful info from the Seeedstudio wiki.
 
IMPORTANT: You need to load your MP3 sounds or songs onto the SDHC card before you install it onto the Serial MP3 player.
 
Once the SDHC card is installed, and your code is uploaded to the Arduino, all you have to do now is connect the MP3 player to some headphones or a powered speaker. You can then power the Arduino and modules with a battery pack or some other portable power supply.
 
You can design and decorate the shoebox in any way you like. Just print out your picture, glue them on, and before you know it, you will have your very own Arduino Boombox.
 


Comments

I was very surprised by the quality of the sound that came from the MP3 module. It is actually quite good.

This tutorial was an introduction to the Grove Serial MP3 module in it's most basic form. You could just as easily use some other sensor to trigger the MP3 module. For example, you could get it to play an alert if a water leak was detected, or if a door was opened, or if the temperature got too high or too low. You could get it to play a reminder when you walk into your room. The possibilities are endless.

I really liked this module, and I am sure it will appear in a future tutorial.


 



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.

Pac-Man Clock Eats Time, Not Pellets

[Bob’s] Pac-Man clock is sure to appeal to the retro geek inside of us all. With a tiny display for the time, it’s clear that this project is more about the art piece than it is about keeping the time. Pac-Man periodically opens and closes his mouth at random intervals. The EL wire adds a nice glowing touch as well.

The project runs off of a Teensy 2.0. It’s a small and inexpensive microcontroller that’s compatible with Arduino. The Teensy uses an external real-time clock module to keep accurate time. It also connects to a seven segment display board via Serial. This kept the wiring simple and made the display easy to mount. The last major component is the servo. It’s just a standard servo, mounted to a customized 3D printed mounting bracket. When the servo rotates in one direction the mouth opens, and visa versa. The frame is also outlined with blue EL wire, giving that classic Pac-Man look a little something extra.

The physical clock itself is made almost entirely from wood. [Bob] is clearly a skilled wood worker as evidenced in the build video below. The Pac-Man and ghosts are all cut on a scroll saw, although [Bob] mentions that he would have 3D printed them if his printer was large enough. Many of the components are hot glued together. The electronics are also hot glued in place. This is often a convenient mounting solution because it’s relatively strong but only semi-permanent.

[Bob] mentions that he can’t have the EL wire and the servo running at the same time. If he tries this, the Teensy ends up “running haywire” after a few minutes. He’s looking for suggestions, so if you have one be sure to leave a comment.


Filed under: Arduino Hacks, clock hacks

433 MHz RF module with Arduino Tutorial 1


There are 4 parts to this tutorial:
To get the most out of this tutorial - it is best to start at tutorial Part 1, and then progress to Part 2 then Part 3 and then do Part 4 last. Doing the RF tutorials in this order will help you to understand the process better.



If you are looking for a way to communicate between Arduinos, but don't have much cash at your disposal, then look no further. These RF modules are not only affordable, but easy to use. They are much easier to set up than an XBee, plus you can use them without the need of a special shield. Before you rush out and buy a ton of these modules, make sure that you are not breaking any radio transmission laws in your country. Do your research, and buy them only if you are allowed to use them in your area. There are a few [OPTIONAL] libraries that can be used to help you and your particular project.


I will mention at this point however, that I did NOT use any libraries in this particular tutorial. That's right. I will show how easy it is to transmit data from one arduino to another using these RF modules WITHOUT libraries.

Also if you are looking for an easy way to record the signals and play them back without a computer - then jump to this tutorial.

Video





Project 1- RF Blink


Firstly we need to test if the RF modules are working. So we will design a very simple transmit and receive sketch to test their functionality. We will use the Arduino's onboard LED to show when the transmitter is transmitting, and when the other Arduino is receiving. There will be a slight delay between the two Arduinos. You can solder an antenna onto these modules, however I did not do this, I just kept the modules close together (1-2cm apart). I also found that I was getting better accuracy when I used 3V instead of 5V to power the receiver. While using 5V for VCC on the receiver, I would get a lot of interference, however with 3V, I hardly got any noise. If you find you are getting unpredictable results, I would suggest you switch to 3V on the receiver and move the transmitter and receiver modules right next to each other. Remember this is just a check... you can experiment with an antenna or a greater distance afterwards.

Here are the parts that you will need to carry out this project:
 

Parts Required



 

The Transmitter and Receiver Fritzing Sketch






The Transmitter

The transmitter has 3 pins




 Notice the pin called "ATAD". It took me a while to figure out what ATAD stood for, when I suddenly realised that this was just a word reversed. It should be DATA (not ATAD). Nevertheless, this is the pin responsible for transmitting the signal. We will make the Arduino's onboard LED illuminate when the transmitter pin is HIGH, and go off when LOW as described in the following table.

 
 



And this is the Arduino Sketch to carry out the data transmission.




Arduino sketch - Transmitter





 

The Receiver



If all goes to plan, the onboard LED on this Arduino should light up (and go off) at the same time as the onboard LED on the transmitting Arduino. There is a chance that the receiver may pick up stray signals from other transmitting devices using that specific frequency. So you may need to play around with the threshold value to eliminate the "noise". But don't make it too big, or you will eliminate the signal in this experiment. You will also notice a small delay between the two Arduinos.


 

Arduino sketch - Receiver




When a HIGH signal is transmitted to the other Arduino. It will produce an AnalogRead = 0.
When a LOW signal is transmitted, it will produce an AnalogRead = 400.
This may vary depending on on your module, and voltage used.
The signals received can be viewed using the Serial Monitor, and can be copied into a spreadsheet to create a chart like this:




You will notice that the HIGH signal (H) is constant, whereas the LOW signal (L) is getting smaller with each cycle. I am not sure why the HIGH signal produces a Analog reading of "0". I would have thought it would have been the other way around. But you can see from the results that a HIGH signal produces a 0 result and a LOW signal produces a value of 400 (roughly).





Tutorial 2

In tutorial 2, we will receive and display a signal from a Mercator RF Remote Controller for Fan/Light.


Tutorial 3

In tutorial 3 - we use the signal acquired from tutorial 2, and transmit the signal to the fan/light to turn the light on and off.


Tutorial 4

In tutorial 4 - we use the information gathered in the first 3 tutorials and do away with the need for a computer. We will listen for a signal, store the signal, and then play it back by pressing a button. Similar to a universal remote ! No libraries, no sound cards, no computer. Just record signal and play it back. Awesome !!


 
 



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.
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.




© Copyright by ScottC

433 MHz RF module with Arduino Tutorial 1




If you are looking for a way to communicate between Arduinos, but don't have much cash at your disposal, then look no further. These RF modules are not only affordable, but easy to use. They are much easier to set up than an XBee, plus you can use them without the need of a special shield. Before you rush out and buy a ton of these modules, make sure that you are not breaking any radio transmission laws in your country. Do your research, and buy them only if you are allowed to use them in your area. There are a few [OPTIONAL] libraries that can be used to help you and your particular project.


I will mention at this point however, that I did NOT use any libraries in this particular tutorial. That's right. I will show how easy it is to transmit data from one arduino to another using these RF modules WITHOUT libraries.

Also if you are looking for an easy way to record the signals and play them back without a computer - then jump to this tutorial.

Video





Project 1- RF Blink


Firstly we need to test if the RF modules are working. So we will design a very simple transmit and receive sketch to test their functionality. We will use the Arduino's onboard LED to show when the transmitter is transmitting, and when the other Arduino is receiving. There will be a slight delay between the two Arduinos. You can solder an antenna onto these modules, however I did not do this, I just kept the modules close together (1-2cm apart). I also found that I was getting better accuracy when I used 3V instead of 5V to power the receiver. While using 5V for VCC on the receiver, I would get a lot of interference, however with 3V, I hardly got any noise. If you find you are getting unpredictable results, I would suggest you switch to 3V on the receiver and move the transmitter and receiver modules right next to each other. Remember this is just a check... you can experiment with an antenna or a greater distance afterwards.

Here are the parts that you will need to carry out this project:
 

Parts Required



 

The Transmitter and Receiver Fritzing Sketch






The Transmitter

The transmitter has 3 pins,
Notice the pin called "ATAD". It took me a while to figure out what ATAD stood for, when I suddenly realised that this was just a word reversed. It should be DATA (not ATAD). Nevertheless, this is the pin responsible for transmitting the signal. We will make the Arduino's onboard LED illuminate when the transmitter pin is HIGH, and go off when LOW as described in the following table.

 

And this is the Arduino Sketch to carry out the data transmission.

Arduino sketch - Transmitter

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
 /* 
RF Blink - Transmit sketch 
    Written by ScottC 17 Jun 2014
    Arduino IDE version 1.0.5
    Website: http://arduinobasics.blogspot.com
    Transmitter: FS1000A/XY-FST
    Description: A simple sketch used to test RF transmission.          
------------------------------------------------------------- */

#define rfTransmitPin 4  //RF Transmitter pin = digital pin 4
#define ledPin 13        //Onboard LED = digital pin 13

void setup(){
  pinMode(rfTransmitPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
}

void loop(){
  for(int i=4000; i>5; i=i-(i/3)){
    digitalWrite(rfTransmitPin, HIGH); //Transmit a HIGH signal
    digitalWrite(ledPin, HIGH); //Turn the LED on
    delay(2000); //Wait for 1 second
    
    digitalWrite(rfTransmitPin,LOW); //Transmit a LOW signal
    digitalWrite(ledPin, LOW); //Turn the LED off
    delay(i); //Variable delay
  }
}




 

The Receiver



If all goes to plan, the onboard LED on this Arduino should light up (and go off) at the same time as the onboard LED on the transmitting Arduino. There is a chance that the receiver may pick up stray signals from other transmitting devices using that specific frequency. So you may need to play around with the threshold value to eliminate the "noise". But don't make it too big, or you will eliminate the signal in this experiment. You will also notice a small delay between the two Arduinos.


 

Arduino sketch - Receiver

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
 /* 
 RF Blink - Receiver sketch 
    Written by ScottC 17 Jun 2014
    Arduino IDE version 1.0.5
    Website: http://arduinobasics.blogspot.com
    Receiver: XY-MK-5V
    Description: A simple sketch used to test RF transmission/receiver.          
------------------------------------------------------------- */

#define rfReceivePin A0  //RF Receiver pin = Analog pin 0
#define ledPin 13        //Onboard LED = digital pin 13

unsigned int data = 0; // variable used to store received data
const unsigned int upperThreshold = 70; //upper threshold value
const unsigned int lowerThreshold = 50; //lower threshold value

void setup(){
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  data=analogRead(rfReceivePin); //listen for data on Analog pin 0
  
  if(data>upperThreshold){
    digitalWrite(ledPin, LOW); //If a LOW signal is received, turn LED OFF
    Serial.println(data);
  }
  
  if(data<lowerThreshold){
    digitalWrite(ledPin, HIGH); //If a HIGH signal is received, turn LED ON
    Serial.println(data);
  }
}




When a HIGH signal is transmitted to the other Arduino. It will produce an AnalogRead = 0.
When a LOW signal is transmitted, it will produce an AnalogRead = 400.
This may vary depending on on your module, and voltage used.
The signals received can be viewed using the Serial Monitor, and can be copied into a spreadsheet to create a chart like this:




You will notice that the HIGH signal (H) is constant, whereas the LOW signal (L) is getting smaller with each cycle. I am not sure why the HIGH signal produces a Analog reading of "0". I would have thought it would have been the other way around. But you can see from the results that a HIGH signal produces a 0 result and a LOW signal produces a value of 400 (roughly).





Tutorial 2

In tutorial 2, we will receive and display a signal from a Mercator RF Remote Controller for Fan/Light.


Tutorial 3

In tutorial 3 - we use the signal acquired from tutorial 2, and transmit the signal to the fan/light to turn the light on and off.


Tutorial 4

In tutorial 4 - we use the information gathered in the first 3 tutorials and do away with the need for a computer. We will listen for a signal, store the signal, and then play it back by pressing a button. Similar to a universal remote ! No libraries, no sound cards, no computer. Just record signal and play it back. Awesome !!


 
 



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.
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.

© Copyright by ScottC

Using older Noritake Itron VFD modules

Introduction

Now and again you come across interesting parts on ebay, from friends or just rooting around in second-hand stores. One example of this was a huge Noritake Itron 40 x 2 character vacuum-fluorescent display from 1994 (or earlier) which was passed on from a client. Originally it looked quite complex, however after spending some time the data sheets were found and it was discovered to have a simple serial interface – and with a little work we’ve got it working, so read on if you’re interested in classic VFDs or have a similar unit.

Getting Started

The model number for our display is CU40026SCPB-T20A. Here’s a quick walk-around, the front:

… the back:

… the interfaces:

… and configuration jumpers:

The serial interface baud rate is determined by the jumpers (above), for example:

So comparing the table above against the jumpers on our module gives us a data speed of 19200 bps with no parity. Great – we can easily create such a connection with a microcontroller with a serial output and 5V logic levels; for our examples we’ll use an Arduino-compatible board.

Wiring up the VFD is simple – see the white jumpers labelled CN2 as shown previously. Pin 1 is 5V (you need an external supply that can offer up to 700 mA), pin 2 to Arduino digital pin 7, and pin 3 to Arduino and power supply GND. We use Arduino D7 with software serial instead of TX so that the display doesn’t display garbage when a sketch is being uploaded. Then it’s a matter of simply sending text to the display, for example here’s a quick demonstration sketch:

// Working with Noritake Itron VFD modules - model CU40026SCPB-T20A
// John Boxall 2013

#include <SoftwareSerial.h>
SoftwareSerial VFD(6,7); // RX, TX

void setup()
{
  VFD.begin(19200);
}

void loop()
{
  VFD.print("Hello, world. This is a Noritake VFD "); // You can blast out text 
  do {} while (1);
}

… and the results:

If you’re not keen on the colour or intensity of the display, try some Perspex over the top – for example:

Controlling the display

At this point you’ll need the data sheet, there’s a couple you can download: data sheet onedata sheet two. As you saw previously, writing text is very simple – just use .print functions. However you may want to send individual characters, as well as special commands to control aspects of the display. These are outlined in the data sheet – see the “Software Commands” and “Character Fonts” tables.

If you need to send single commands – for example “clear display” which is 0x0E, use a .write command, such as:

VFD.write(0x0E); // clear display

Some commands are in the format of escape codes (remember those?) so you need to send ESC then the following byte, for example to change the brightness to 50%:

VFD.write(0x1B); // ESC
    VFD.write(0x4C); // brightness
    VFD.write(0x40); // 50% brightness

Armed with that knowledge and the data sheets you can now execute all the commands. According to the data sheet it is possible to change fonts however no matter what the hardware jumper or command we tried it wouldn’t budge from the Japanese katakana font. Your screen may vary. If you use the “screen priority write” function heed the data sheet with respect to the extended “busy” time by delaying subsequent writes to the display by a millisecond.

 Putting it all together

Instead of explaining each and every possible command, I’ve put the common ones inside documented functions in the demonstration sketch below, which is followed by a quick video of the sketch in operation.

// Working with Noritake Itron VFD modules - model CU40026SCPB-T20A
// John Boxall 2013

#include <SoftwareSerial.h>
SoftwareSerial VFD(6,7); // rx, tx

void setup()
{
  VFD.begin(19200); // set speed for software serial port 
  resetVFD();  
  VFDclearsceen();
//  VFD.write(0x12); // vertical scroll mode (on)
}

void resetVFD()
// performs a software reset on the VFD controller
{
  VFD.write(0x1B); // ESC
  VFD.write(0x49); // software reset
}

void VFDnewline()
// moves cursor to start of next line
{
  VFD.write(0x0D); // carriage return
  VFD.write(0x0A); // line feed
}

void VFDclearsceen()
// moves cursor to top-left and clears display
{
  VFD.write(0x0E); // clear display 
  VFD.write(0x0C); // form feed - cursor to top-left
}

void VFDbrightness(int amount)
// sets VFD brightness - 25/50/75/100%
// uses ESC sequences
{
  switch(amount)
  {
  case 25:
    VFD.write(0x1B); // ESC
    VFD.write(0x4C); // brightness
    VFD.print(0); // 25% brightness
    break;
  case 50:
    VFD.write(0x1B); // ESC
    VFD.write(0x4C); // brightness
    VFD.write(0x40); // 50% brightness
    break;
  case 75:
    VFD.write(0x1B); // ESC
    VFD.write(0x4C); // brightness
    VFD.write(0x80); // 75% brightness
    break;
  case 100:
    VFD.write(0x1B); // ESC
    VFD.write(0x4C); // brightness
    VFD.write(0xC0); // 100% brightness
  }
}

void VFDchars()
// run through characters for selected font
{
  for (int i = 21 ; i < 256; i++)
  {
    VFD.write(0x16); // underline cursor off
    VFD.write(i);
    delay(100);
  }
}

void moveCursor(byte position)
// moves the cursor - top row is 0~39, bottom row is 40~79
// vertical scroll mode must be turned off if used
{
    VFD.write(0x1B); // ESC
    VFD.write(0x48); // move cursor 
    VFD.write(position); // location
}

void loop()
{
  VFD.write(0x16); // underline cursor off
  VFD.print("Hello, world - line one."); // You can blast out text 
  delay(1000);      
  VFDnewline();
  VFD.print("Hello, world - line two."); 
  delay(1000);    
  VFDclearsceen();
  VFDbrightness(25);
  VFD.print("*** 25% brightness ***");   
  delay(1000);
  VFDclearsceen();  
  VFDbrightness(50);
  VFD.print("*** 50% brightness ***");     
  delay(1000);
  VFDclearsceen();   
  VFDbrightness(75);
  VFD.print("*** 75% brightness ***");       
  delay(1000);
  VFDclearsceen();   
  VFDbrightness(100);
  VFD.print("*** 100% brightness ***");         
  delay(1000);
  VFDclearsceen();

  VFDchars();
  VFDclearsceen();

  for (int i = 0; i < 80; i++)
  {
    VFD.write(0x16); // underline cursor off
    moveCursor(i);
    VFD.print("X");
    delay(100);
    moveCursor(i);    
    VFD.print(" ");    
  }
  VFDclearsceen();
}

 

Conclusion

We hope you found this interesting and helpful. And if you have an inexpensive source for these old displays, let us know in the comments. Full-sized images are on flickr. And if you made it this far – check out my new book “Arduino Workshop” from No Starch Press.

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The post Using older Noritake Itron VFD modules appeared first on tronixstuff.

Showcase your project: ListComPorts per Windows

Marco Lai ci propone un software da lui sviluppato per semplificare la gestione delle porte COM su di un sistema Windows, nel caso vengano utilizzate differenti schede Arduino in una volta sola:

Spesso mi capita di usare diversi tipi di Arduino collegati allo stesso pc e diventa abbastanza frustrante capire quale sia la relativa porta COM, dato che nell’IDE viene elencato il nome della porta senza la descrizione del tipo di dispositivo associato.
Quindi devo accedere a gestione dispositivi e controllare nella voce Port (COM e LPT) quale sia questa associazione, in modo da caricare gli sketch sui giusto relativi dispositivi.
Il problema poi aumenta quando collego e scollego le schede dalla porta usb, magari invertendo l’ordine sulle porte, ottenendo così nuovi assegnamenti di COM che mi costringono a tenere aperta la finestra gestione dispositivi.
Ho scritto una piccola utility che elenca le porte COM con la descrizione del dispositivo collegato, in questo modo lavoro meglio e quando ricollego le schede posso cliccare sul pulsante aggiorna per ottenere la nuova lista aggiornata.

Maggiori informazioni, così come il link per scaricare ListComPorts, possono essere trovate qui.

Arduino Blog 15 Feb 16:00
com  port  serial  software