{"id":209,"date":"2016-01-01T22:52:17","date_gmt":"2016-01-01T22:52:17","guid":{"rendered":"http:\/\/thedutchguys.com\/?p=209"},"modified":"2022-05-09T08:06:02","modified_gmt":"2022-05-09T06:06:02","slug":"sonar","status":"publish","type":"post","link":"https:\/\/thijsbekke.nl\/blog\/2016\/01\/01\/sonar\/","title":{"rendered":"Sonar"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Leestijd: <\/span> <span class=\"rt-time\">5<\/span> <span class=\"rt-label rt-postfix\">Minuten<\/span><\/span><p>Processing view van de HC-SR04<\/p>\n<p>De\u00a0HC-SR04 uit nieuwsgierigheid een keer besteld op internet. Daarna verdween hij heel lang in de la zonder er wat mee gedaan te hebben. Tot aan vandaag.<\/p>\n<p>Wat is de HC-SR04 ? Het is een Ultrasone sensor module. Het kan volgens de specs objecten &#8220;zien&#8221; vanaf 2 centimeter tot 400 centimeter. Met een precisie van 3 mm. De module bevat de zender, de ontvaner en het circuit om alles te regelen. Wat over blijft zijn 4 pinnen die je aanstuurt met bijvoorbeeld je Arduino. De module stuurt een ultrasone geluid richting het object die het terugkaatst. Dit geluid wordt vervolgens opgevangen door zijn eigen sensoren. De tijd tussen het versturen en ontvangen wordt terug gegeven.\u00a0<!--more--><\/p>\n<p>Wanneer je het standaard voorbeeld &#8220;Ping&#8221; van de Arduino software een klein beetje aanpast werkt het ook voor de\u00a0HC-SR04. \u00a0Je moet een extra pin defini\u00ebren waar je\u00a0minimaal 2 microseconden een pulse naar toe stuurt.\u00a0Op deze manier geeft de module op de &#8220;echo pin&#8221; een pulse waaruit je de lengte in centimeters kan berekenen.<\/p>\n<p>De snelheid van geluid is 340 meter per seconden, oftewel 29 microseconden per centimeter. Wanneer je de pulse deelt door 29, heb je de afgelegde afstand in centimeters. Vervolgens deel je dit door 2 aangezien het ultrasone geluid heen en terug gaat.<\/p>\n<pre class=\"lang:arduino decode:true\">\/* HC-SR04 Sensor\n   The circuit:\n * VCC connection of the sensor attached to +5V\n * GND connection of the sensor attached to ground\n * TRIG connection of the sensor attached to digital pin 2\n * ECHO connection of the sensor attached to digital pin 4\n *\/\n  \nconst int trigPin = 2;\nconst int echoPin = 4;\n \nvoid setup() {\n  \/\/ initialize serial communication:\n  Serial.begin(9600);\n  pinMode(trigPin, OUTPUT);\n  pinMode(echoPin, INPUT);\n}\n \nvoid loop()\n{\n  \/\/ establish variables for duration of the ping, \n  \/\/ and the distance result in inches and centimeters:\n  long duration, cm;\n \n  \/\/ The sensor is triggered by a HIGH pulse of 10 or more microseconds.\n  \/\/ Give a short LOW pulse beforehand to ensure a clean HIGH pulse:\n  \n  digitalWrite(trigPin, LOW);\n  delayMicroseconds(2);\n  digitalWrite(trigPin, HIGH);\n  delayMicroseconds(10);\n  digitalWrite(trigPin, LOW);\n \n   \/\/ Read the signal from the sensor: a HIGH pulse whose\n  \/\/ duration is the time (in microseconds) from the sending\n  \/\/ of the ping to the reception of its echo off of an object.\n  duration = pulseIn(echoPin, HIGH);\n \n  \/\/ convert the time into a distance\n  cm = microsecondsToCentimeters(duration);\n  \n  Serial.print(cm);\n  Serial.print(\"cm\");\n  Serial.println();\n  \n  delay(100);\n}\n\n \nlong microsecondsToCentimeters(long microseconds)\n{\n  \/\/ The speed of sound is 340 m\/s or 29 microseconds per centimeter.\n  \/\/ The ping travels out and back, so to find the distance of the\n  \/\/ object we take half of the distance travelled.\n  return microseconds \/ 29 \/ 2;\n}<\/pre>\n<p>Ik kwam een <a href=\"https:\/\/www.youtube.com\/watch?v=jo1mO9aXBy8\">YouTube <\/a>filmpje tegen die deze module op een servo had geplaatst en vervolgens de data terugstuurde naar Processing om er volgens een &#8220;Radar&#8221; beeld van te maken.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-213 size-full\" src=\"https:\/\/thijsbekke.nl\/blog\/wp-content\/uploads\/2016\/01\/IMG_20160101_222552.jpg\" alt=\"IMG_20160101_222552\" width=\"3200\" height=\"1824\" \/><\/p>\n<pre class=\"lang:arduino decode:true \">\/* HC-SR04 Sensor\n   The circuit:\n  * VCC connection of the sensor attached to +5V\n  * GND connection of the sensor attached to ground\n  * TRIG connection of the sensor attached to digital pin 2\n  * ECHO connection of the sensor attached to digital pin 4\n  * Servo connected to pin 9\n  *\/\n \n#include &lt;Servo.h&gt; \n\nServo servo;\n\nstatic const int minAngle = 0;\nstatic const int maxAngle = 180;\nint angle=0;\nint servoAngle;\nint servoPos;\nconst int servoPin = 9;\nconst int trigPin = 2;\nconst int echoPin = 4;\n \nvoid setup() {\n  \/\/ initialize serial communication:\n Serial.begin(9600);\n\n servo.attach(servoPin);\n \n pinMode(trigPin, OUTPUT);\n pinMode(echoPin, INPUT);\n pinMode(trigPin, OUTPUT);\n pinMode(echoPin, INPUT);\n}\n \nvoid loop()\n{\n  if (Serial.available()) {\n    angle = Serial.parseInt();\n    if(angle&gt;-1){\n      servoPos = constrain(map(angle, 0,180,minAngle,maxAngle),minAngle,maxAngle);\n      servo.write(servoPos);\n      getDistance();\n    }\n  }\n}\n\nvoid getDistance(){ \n  \/\/ establish variables for duration of the ping, \n  \/\/ and the distance result in inches and centimeters:\n  long duration, inches, cm;\n \n  \/\/ The sensor is triggered by a HIGH pulse of 10 or more microseconds.\n  \/\/ Give a short LOW pulse beforehand to ensure a clean HIGH pulse:\n \n  digitalWrite(trigPin, LOW);\n  delayMicroseconds(2);\n  digitalWrite(trigPin, HIGH);\n  delayMicroseconds(10);\n  digitalWrite(trigPin, LOW);\n \n  \/\/ Read the signal from the sensor: a HIGH pulse whose\n  \/\/ duration is the time (in microseconds) from the sending\n  \/\/ of the ping to the reception of its echo off of an object.\n  duration = pulseIn(echoPin, HIGH);\n \n  \/\/ convert the time into a distance\n  cm = microsecondsToCentimeters(duration);\n\n  Serial.print(cm);\n  Serial.println();\n  \n  delay(100);\n}\n \nlong microsecondsToCentimeters(long microseconds)\n{\n  \/\/ The speed of sound is 340 m\/s or 29 microseconds per centimeter.\n  \/\/ The ping travels out and back, so to find the distance of the\n  \/\/ object we take half of the distance travelled.\n  return microseconds \/ 29 \/ 2;\n}\n<\/pre>\n<div id=\"attachment_212\" style=\"width: 940px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-212\" loading=\"lazy\" class=\"wp-image-212 size-full\" src=\"https:\/\/thijsbekke.nl\/blog\/wp-content\/uploads\/2016\/01\/Breadboard_servo.png\" alt=\"Aansluitschema HC-SR04 met Servo\" width=\"930\" height=\"784\" \/><p id=\"caption-attachment-212\" class=\"wp-caption-text\">Aansluitschema HC-SR04 met Servo<\/p><\/div>\n<p>Het resultaat een grafische weergave van de gegevens in Processing.<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-216\" src=\"https:\/\/thijsbekke.nl\/blog\/wp-content\/uploads\/2016\/01\/processing.png\" alt=\"Processing view van de HC-SR04\" width=\"1036\" height=\"804\" \/><\/p>\n<p>En uiteindelijk nog een filmpje van de\u00a0HC-SR04 in actie.<\/p>\n<p><a href=\"https:\/\/youtu.be\/KxpdmIILugQ\">https:\/\/youtu.be\/KxpdmIILugQ<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Leestijd: <\/span> <span class=\"rt-time\">5<\/span> <span class=\"rt-label rt-postfix\">Minuten<\/span><\/span> Processing view van de HC-SR04 De\u00a0HC-SR04 uit nieuwsgierigheid een keer besteld op internet. Daarna verdween hij heel lang in de la zonder er wat mee gedaan te hebben. Tot aan vandaag. Wat is de HC-SR04 ? Het is een Ultrasone sensor module. Het kan volgens de specs objecten &#8220;zien&#8221; vanaf 2 centimeter tot 400 centimeter. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":210,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3],"_links":{"self":[{"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/posts\/209"}],"collection":[{"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":6,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":635,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/posts\/209\/revisions\/635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/media\/210"}],"wp:attachment":[{"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}