{"id":245,"date":"2017-04-25T14:29:56","date_gmt":"2017-04-25T14:29:56","guid":{"rendered":"http:\/\/martindebie.com\/workshop\/?p=245"},"modified":"2020-11-14T16:44:03","modified_gmt":"2020-11-14T16:44:03","slug":"arduino","status":"publish","type":"post","link":"https:\/\/martindebie.com\/workshop\/arduino\/","title":{"rendered":"ARDUINO"},"content":{"rendered":"\n<p>Arduino is an open-source electronics platform based on easy-to-use hardware and software.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ARDUINO INTRODUCTION<\/h3>\n\n\n\n<iframe loading=\"lazy\" src=\"https:\/\/docs.google.com\/presentation\/d\/e\/2PACX-1vRWZYzEu2grnJrzx3Q0FNDJvNGJFd1ilHbLBFfK_b0jZdOKJQRqvEggqLFG04sZpZwHM0cM7GkpMB2u\/embed?start=false&#038;loop=false&#038;delayms=3000\" frameborder=\"0\" width=\"960\" height=\"569\" allowfullscreen=\"true\" mozallowfullscreen=\"true\" webkitallowfullscreen=\"true\"><\/iframe>\n\n\n\n<figure class=\"wp-block-embed-vimeo wp-block-embed is-type-video is-provider-vimeo wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Arduino The Documentary (2010) English HD\" src=\"https:\/\/player.vimeo.com\/video\/18539129?dnt=1&amp;app_id=122963\" width=\"940\" height=\"529\" frameborder=\"0\" allow=\"autoplay; fullscreen; picture-in-picture; clipboard-write\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">HOW TO CONNECT A SENSOR<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"458\" height=\"359\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2017\/04\/potentiometer-fig-1.png\" alt=\"\" class=\"wp-image-2206\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2017\/04\/potentiometer-fig-1.png 458w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2017\/04\/potentiometer-fig-1-300x235.png 300w\" sizes=\"auto, (max-width: 458px) 100vw, 458px\" \/><\/figure>\n\n\n\n<p>Potentiometer have three legs (+, data, -) but in the case of a sensor with only  two legs we will use a 10k ohm resistance to create a voltage divider.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"137\" height=\"176\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/Impedance_voltage_divider.png\" alt=\"\" class=\"wp-image-2220\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">CODE EXAMPLE USING A SENSOR TO DIM A LED<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ These constants won't change:\nconst int analogInPin = A0;  \/\/ Analog input pin that the sensor is attached to\nconst int analogOutPin = 9; \/\/ Analog output pin that the LED is attached to\n\nint sensorValue = 0;        \/\/ value read from the sensor\nint outputValue = 0;        \/\/ value output to the PWM (analog out)\n\nvoid setup() {\n  \/\/ initialize serial communications at 9600 bps:\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  \/\/ read the analog in value:\n  sensorValue = analogRead(analogInPin);\n  \/\/ map it to the range of the analog out:\n  outputValue = map(sensorValue, 0, 1023, 0, 255);\n  \/\/ change the analog out value:\n  analogWrite(analogOutPin, outputValue);\n\n  \/\/ print the results to the serial monitor:\n  Serial.print(\"sensor = \");\n  Serial.print(sensorValue);\n  Serial.print(\"\\t output = \");\n  Serial.println(outputValue);\n\n  \/\/ wait 2 milliseconds before the next loop\n  \/\/ for the analog-to-digital converter to settle\n  \/\/ after the last reading:\n  delay(2);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">DIFFERENT TYPE OF SENSORS<\/h3>\n\n\n\n<p>This first sensor is just a simple button, sending on\/off informations<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"250\" height=\"245\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/switch-250x245-1.jpg\" alt=\"\" class=\"wp-image-2207\"\/><figcaption>Switch<\/figcaption><\/figure><\/div>\n\n\n\n<p>For this other sensors, arduino is reading the variation of their resistance<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"480\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/potentiometer.jpg\" alt=\"\" class=\"wp-image-2208\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/potentiometer.jpg 480w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/potentiometer-300x300.jpg 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/potentiometer-150x150.jpg 150w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><figcaption>Potentiometer<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"600\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/fsr.jpg\" alt=\"\" class=\"wp-image-2209\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/fsr.jpg 600w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/fsr-300x300.jpg 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/fsr-150x150.jpg 150w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption>FSR (Force sensing resistance)<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"600\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/bend.jpg\" alt=\"\" class=\"wp-image-2211\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/bend.jpg 600w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/bend-300x300.jpg 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/bend-150x150.jpg 150w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption>Bend or flex sensor<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"360\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/ldr.jpg\" alt=\"\" class=\"wp-image-2212\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/ldr.jpg 360w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/ldr-300x300.jpg 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/ldr-150x150.jpg 150w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><figcaption>LDR (Light dependant resistance)<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/rain.jpg\" alt=\"\" class=\"wp-image-2213\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/rain.jpg 500w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/rain-300x300.jpg 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/rain-150x150.jpg 150w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><figcaption>Rain sensor<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/moisture.jpg\" alt=\"\" class=\"wp-image-2214\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/moisture.jpg 500w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/moisture-300x300.jpg 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/moisture-150x150.jpg 150w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><figcaption>Moisture sensor<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"1000\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/level.jpg\" alt=\"\" class=\"wp-image-2215\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/level.jpg 1000w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/level-300x300.jpg 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/level-150x150.jpg 150w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/level-768x768.jpg 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption>Water level sensor<\/figcaption><\/figure><\/div>\n\n\n\n<p>This sensors are sending digital informations<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"600\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/sharp.jpg\" alt=\"\" class=\"wp-image-2216\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/sharp.jpg 600w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/sharp-300x300.jpg 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/sharp-150x150.jpg 150w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption>Distance infra-red sensor<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"600\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/accelero.jpg\" alt=\"\" class=\"wp-image-2217\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/accelero.jpg 600w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/accelero-300x300.jpg 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/accelero-150x150.jpg 150w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption>Accelerometer<\/figcaption><\/figure><\/div>\n\n\n\n<p>This sensor uses the piezoelectric effect, to measure changes in pressure, acceleration, temperature, strain, or force by converting them to an electrical charge.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"600\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/piezo.png\" alt=\"\" class=\"wp-image-2218\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/piezo.png 600w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/piezo-300x300.png 300w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/piezo-150x150.png 150w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption>Piezoelectric sensor<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">HOW TO MAKE SOUND WITH ARDUINO<\/h3>\n\n\n\n<p>There are a library called Tone, that we can use for making melody.<\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\">Syntax<\/span><\/p>\n\n\n\n<p>tone(pin, frequency)<br>tone(pin, frequency, duration)<\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-red-color\">Parameters<\/span><\/p>\n\n\n\n<p>pin: the pin on which to generate the tone<br>frequency: the frequency of the tone in hertz &#8211; unsigned int<br>duration: the duration of the tone in milliseconds (optional) &#8211; unsigned long<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"760\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/16099797817_4f1e1d2bba_o.png\" alt=\"\" class=\"wp-image-2221\" srcset=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/16099797817_4f1e1d2bba_o.png 580w, https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/16099797817_4f1e1d2bba_o-229x300.png 229w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span class=\"has-inline-color has-vivid-red-color\">Board<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Min frequency (Hz)<\/span><\/td><td><span class=\"has-inline-color has-vivid-red-color\">Max frequency (Hz)<\/span><\/td><\/tr><tr><td>Uno, Mega, Leonardo and other AVR boards<\/td><td>31<\/td><td>65535<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>A code example of an arduino &#8220;theremin&#8221; using a LDR and a speaker with tone library<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ These constants won't change:\nconst int LDR = A0; \/\/ pin that the sensor is attached to\nconst int Speaker = 9; \/\/ pin that the LED is attached to\n\n\/\/ variables:\nint sensorValue = 0; \/\/ the sensor value\nint sensorMin = 1023; \/\/ minimum sensor value\nint sensorMax = 0; \/\/ maximum sensor value\nvoid setup() {\n\/\/ turn on LED to signal the start of the calibration period:\npinMode(13, OUTPUT);\ndigitalWrite(13, HIGH);\n\n\/\/ calibrate during the first five seconds\nwhile (millis() &amp;amp;lt; 5000) { sensorValue = analogRead(LDR); \/\/ record the maximum sensor value if (sensorValue &amp;amp;gt; sensorMax) {\nsensorMax = sensorValue;\n}\n\n\/\/ record the minimum sensor value\nif (sensorValue &amp;amp;lt; sensorMin) {\nsensorMin = sensorValue;\n}\n}\n\n\/\/ signal the end of the calibration period\ndigitalWrite(13, LOW);\n}\n\nvoid loop() {\n\/\/ read the sensor:\nsensorValue = analogRead(LDR);\n\n\/\/ apply the calibration to the sensor reading\nsensorValue = map(sensorValue, sensorMin,\nsensorMax, 50, 4000);\n\n\/\/ play the tone\ntone(Speaker, sensorValue);\n\n\/\/Take a break\ndelay(10);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">CREATE A CAPACITIVE SENSOR WITH ARDUINO<\/h3>\n\n\n\n<p>For this you need the library <a href=\"https:\/\/github.com\/arduino-libraries\/CapacitiveSensor\/zipball\/master\">capacitiveSensor<\/a>. You will find it inside the top menu Sketch\/Include library\/manage library\/&#8230;<\/p>\n\n\n\n<p>Connect your sensor to arduino :<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"345\" height=\"320\" src=\"https:\/\/martindebie.com\/workshop\/wp-content\/uploads\/2020\/11\/CapSense.gif\" alt=\"\" class=\"wp-image-2219\"\/><\/figure>\n\n\n\n<p><strong>Which resistor to use<\/strong><br>Use a 1 megohm resistor (or less maybe) for absolute touch to activate.<br>With a 10 megohm resistor the sensor will start to respond 10-15 centimeters away.<br>With a 40 megohm resistor the sensor will start to respond 30-60 centimeters away (dependent on the foil size). Common resistor sizes usually end at 10 megohm so you may have to solder four 10 megohm resistors end to end.<br>One tradeoff with larger resistors is that the sensor&#8217;s increased sensitivity means that it is slower. Also if the sensor is exposed metal, it is possible that the send pin will never be able to force a change in the receive (sensor) pin, and the sensor will timeout.<br>Also experiment with small capacitors (100 pF &#8211; .01 uF) to ground, on the sense pin. They improve stability of the sensor.<\/p>\n\n\n\n<p>Code for capacitive sensor :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &amp;amp;amp;lt;CapacitiveSensor.h&amp;amp;amp;gt;\n\nCapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        \/\/ 10M resistor between pins 4 &amp;amp;amp;amp; 2, pin 2 is sensor pin, add a wire and or foil if desired\n\nvoid setup()\n{\n   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     \/\/ turn off autocalibrate on channel 1 - just as an example\n   Serial.begin(9600);\n}\n\nvoid loop()\n{\n    long start = millis();\n    long total1 =  cs_4_2.capacitiveSensor(30);\n\n    Serial.print(millis() - start);        \/\/ check on performance in milliseconds\n    Serial.print(\"\\t\");                    \/\/ tab character for debug windown spacing\n    Serial.println(total1);                  \/\/ print sensor output 1\n\n    delay(10);                             \/\/ arbitrary delay to limit data to serial port\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p><\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Arduino is an open-source electronics platform based on easy-to-use hardware and software. ARDUINO INTRODUCTION HOW&#8230;<\/p>\n","protected":false},"author":1,"featured_media":246,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":8,"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":2225,"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/posts\/245\/revisions\/2225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/media\/246"}],"wp:attachment":[{"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/martindebie.com\/workshop\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}