{"id":87,"date":"2016-03-08T17:56:41","date_gmt":"2016-03-08T13:56:41","guid":{"rendered":"http:\/\/elmeranta.fi\/blog\/?p=87"},"modified":"2018-04-13T16:54:44","modified_gmt":"2018-04-13T12:54:44","slug":"distance-sensor-with-led-matrix-display-on-arduino-nano","status":"publish","type":"post","link":"https:\/\/elmeranta.fi\/blog\/2016\/03\/distance-sensor-with-led-matrix-display-on-arduino-nano\/","title":{"rendered":"Distance sensor with LED matrix display on Arduino Nano"},"content":{"rendered":"<p>First things first, the end product can be viewed here:<br \/>\n<iframe loading=\"lazy\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/yUFA-6YolO0?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h3>Parts<\/h3>\n<div id=\"attachment_92\" style=\"width: 310px\" class=\"wp-caption alignright\"><a href=\"http:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_distance_sensor_parts.jpg\" rel=\"attachment wp-att-92\"><img aria-describedby=\"caption-attachment-92\" loading=\"lazy\" class=\"wp-image-92 size-medium\" src=\"http:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_distance_sensor_parts-300x190.jpg\" alt=\"distance_sensing_arduino_nano_distance_sensor_parts\" width=\"300\" height=\"190\" srcset=\"https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_distance_sensor_parts-300x190.jpg 300w, https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_distance_sensor_parts-768x488.jpg 768w, https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_distance_sensor_parts.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-92\" class=\"wp-caption-text\"><br \/>All the parts. From top: HC-SR04 sensor, 8&#215;8 LED matrix display, Arduino Nano (clone)<\/p><\/div>\n<ul>\n<li>Arduino (Nano, Uno, whatever fits your need) with a power source<\/li>\n<li>HC-SR04 distance sensor<\/li>\n<li>MAX7219 based 8&#215;8 LED matrix display<\/li>\n<li>9 connection wires<\/li>\n<\/ul>\n<h3>Wiring<\/h3>\n<p>Lets start with the 8&#215;8 display:<br \/>\n<a href=\"http:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_8x8_led_matrix.jpg\" rel=\"attachment wp-att-91\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-91\" src=\"http:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_8x8_led_matrix-300x225.jpg\" alt=\"distance_sensing_8x8_led_matrix\" width=\"300\" height=\"225\" srcset=\"https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_8x8_led_matrix-300x225.jpg 300w, https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_8x8_led_matrix-768x576.jpg 768w, https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_8x8_led_matrix.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<ul>\n<li>VCC to Nano 5V (white)<\/li>\n<li>GND to Nano GND (black)<\/li>\n<li>DIN to Nano Digital pin 12 (gray)<\/li>\n<li>CS to Nano Digital pin 10 (purple)<\/li>\n<li>CLK to nano Digital pin 11 (blue)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>And then the distance sensor. Since it needs 5V and ground connections too, we&#8217;re using the 8&#215;8 LED display&#8217;s output pins. They are there to make chaining the 8&#215;8 displays easy, but can be naturally used for other sensors, too. Pin order is same as on the input ones.<br \/>\n<a href=\"http:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_hc-sr04_sensor.jpg\" rel=\"attachment wp-att-94\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-94\" src=\"http:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_hc-sr04_sensor-300x225.jpg\" alt=\"distance_sensing_hc-sr04_sensor\" width=\"300\" height=\"225\" srcset=\"https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_hc-sr04_sensor-300x225.jpg 300w, https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_hc-sr04_sensor-768x576.jpg 768w, https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_hc-sr04_sensor.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<ul>\n<li>VCC to 8&#215;8 LED display VCC out (red)<\/li>\n<li>GND to 8&#215;8 LED display GND out (gray)<\/li>\n<li>Trig to Nano Digital pin 9 (brown)<\/li>\n<li>Echo to Nano Digital pin 8 (purple, again \ud83d\ude09 )<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>And how they look like on the Nano:<\/p>\n<p><a href=\"http:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_wiring.jpg\" rel=\"attachment wp-att-93\"><img loading=\"lazy\" class=\"alignnone wp-image-93 size-medium\" src=\"http:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_wiring-225x300.jpg\" alt=\"distance_sensing_arduino_nano_wiring\" width=\"225\" height=\"300\" srcset=\"https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_wiring-225x300.jpg 225w, https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_wiring-768x1024.jpg 768w, https:\/\/elmeranta.fi\/blog\/wp-content\/uploads\/2016\/03\/distance_sensing_arduino_nano_wiring.jpg 1024w\" sizes=\"(max-width: 225px) 100vw, 225px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Code<\/h3>\n<p>This is my &#8220;first draft&#8221; code which is still under development (and uses example code for initialization, etc from LedControl examples, etc)..<\/p>\n<p>The code uses LedControl library which can be installed through the compiler or downloaded from <a href=\"https:\/\/github.com\/wayoda\/LedControl\" target=\"_blank\">here<\/a>. Note that the pins can be changed if you need to use a different configuration.<\/p>\n<p>For actual use, the delay with water level reading should be set to minutes and any obviously erroneous readings should result into a re-read. (Sometimes the sensor reading will result in the display blanking out until it can get a &#8220;good&#8221; reading.)<\/p>\n<pre>\/\/We always have to include the library\r\n#include \"LedControl.h\"\r\n\/\/ Distance sensor pins\r\n#define trigPin 9\r\n#define echoPin 8\r\n\/*\r\n\u00a0pin 12 is connected to the DataIn \r\n\u00a0pin 11 is connected to the CLK \r\n\u00a0pin 10 is connected to LOAD \r\n*\/\r\nLedControl lc=LedControl(12,11,10,1);\r\n\r\n\/* we always wait a bit between updates of the display *\/\r\nunsigned long delaytime=100;\r\nunsigned int cnt = 0;\r\n\/* sensor calibration from fluid level, in CM\r\n\u00a0\u00a0 This is how far your sensor is from the actual, maximum fluid level *\/\r\nunsigned long calibration = 10;\r\n\r\n\/* bytecode for the 8x8 images *\/\r\nstatic byte waterlevel[][8] = {\r\n{B00000000,B01100101,B10010101,B10010110,B10010110,B10010101,B01100101,B00000000}, \/\/ OK\r\n{B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11111111}, \/\/ water level, low\r\n{B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B10101101,B11111111},\r\n{B00000000,B00000000,B00000000,B00000000,B00000000,B10101101,B11111111,B11111111},\r\n{B00000000,B00000000,B00000000,B00000000,B11010011,B11111111,B11111111,B11111111},\r\n{B00000000,B00000000,B00000000,B10111010,B11111111,B11111111,B11111111,B11111111},\r\n{B00000000,B00000000,B10110101,B11111111,B11111111,B11111111,B11111111,B11111111},\r\n{B00000000,B10110110,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111},\r\n{B11010101,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111},\r\n{B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111} \/\/ water level, high\r\n};\r\n\r\nvoid setup() {\r\n\u00a0 \/*\r\n\u00a0\u00a0 The MAX72XX is in power-saving mode on startup,\r\n\u00a0\u00a0 we have to do a wakeup call\r\n\u00a0\u00a0 *\/\r\n\u00a0 lc.shutdown(0,false);\r\n\u00a0 \/* Set the brightness to a medium values *\/\r\n\u00a0 lc.setIntensity(0,2);\r\n\u00a0 \/* and clear the display *\/\r\n\u00a0 lc.clearDisplay(0);\r\n\r\n\u00a0 \/* Distance sensor *\/\r\n\u00a0 pinMode(trigPin, OUTPUT);\r\n\u00a0 pinMode(echoPin, INPUT);\r\n}\r\n\r\nvoid showlevel(unsigned int cnt) {\r\n\u00a0 unsigned int i=0;\r\n\u00a0\u00a0\u00a0 for (i=0;i&lt;=7;i++) { \/* 8 rows, display 0..7 *\/\r\n\u00a0\u00a0\u00a0 lc.setRow(0,i,waterlevel[cnt][i]);\r\n\u00a0 } \u00a0\r\n}\r\n\r\nvoid loop() { \r\n\u00a0 long duration, distance;\r\n\u00a0 digitalWrite(trigPin, LOW);\u00a0\u00a0 \/\/ get ready to measure\r\n\u00a0 delayMicroseconds(2);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ wait\r\n\u00a0 digitalWrite(trigPin, HIGH);\u00a0 \/\/ send measuring signal\r\n\u00a0 delayMicroseconds(10);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ for 10us\r\n\u00a0 digitalWrite(trigPin, LOW);\u00a0\u00a0 \/\/ end signal\r\n\u00a0 duration = pulseIn(echoPin, HIGH);\u00a0 \/\/ wait for input signal (rebound)\r\n\u00a0 distance = (duration\/2) \/ 29.1;\u00a0\u00a0\u00a0\u00a0 \/\/ calculate speed of sound to meters, speed is ~340ms, ie. ~1.7cm per us\r\n\r\n\u00a0 cnt = 0;\r\n\u00a0 if (distance &lt; 50 + calibration) {cnt++;}\r\n\u00a0 if (distance &lt; 40 + calibration) {cnt++;}\r\n\u00a0 if (distance &lt; 35 + calibration) {cnt++;}\r\n\u00a0 if (distance &lt; 30 + calibration) {cnt++;}\r\n\u00a0 if (distance &lt; 25 + calibration) {cnt++;}\r\n\u00a0 if (distance &lt; 20 + calibration) {cnt++;}\r\n\u00a0 if (distance &lt; 15 + calibration) {cnt++;}\r\n\u00a0 if (distance &lt; 10 + calibration) {cnt++;}\r\n\u00a0 if (distance &lt; 5 + calibration) {cnt++;}\r\n\r\n\u00a0 lc.setIntensity(0,2+cnt\/2); \/\/ increase intensity as distance shortens\r\n\r\n\u00a0 showlevel(cnt); \/\/ draw the image to the display\r\n\u00a0 delay(delaytime); \/\/ wait\r\n\u00a0 lc.clearDisplay(0); \/\/ clear display, or we will have LEDs that were on, stay that way\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The code is also <a href=\"https:\/\/github.com\/docweird\/sounddistance\" target=\"_blank\">available at github<\/a>.<\/p>\n<p>Now all that is needed is to actually package this thing (moisture and cold proof, as I&#8217;m planning to use mine outside all year around) and solve what kind of power supply I will use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First things first, the end product can be viewed here: Parts Arduino (Nano, Uno, whatever fits your need) with a power source HC-SR04 distance sensor MAX7219 based 8&#215;8 LED matrix display 9 connection wires Wiring Lets start with the 8&#215;8 display: VCC to Nano 5V (white) GND to Nano GND (black) DIN to Nano Digital [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":92,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,13],"tags":[16,17],"_links":{"self":[{"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/posts\/87"}],"collection":[{"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":10,"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":101,"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/posts\/87\/revisions\/101"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/media\/92"}],"wp:attachment":[{"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elmeranta.fi\/blog\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}