Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2K views

Sonar With Arduino, C#, JavaScript and HTML5

This document describes a project to create a sonar device using an Arduino, ultrasonic sensor, servo motor, and software components. The Arduino sketches measures distance with the ultrasonic sensor and controls the servo motor position. It sends the angle and distance data to a .NET/C# console app via serial. The app broadcasts the data to an HTML5/JavaScript client using SignalR which displays a sonar image on a canvas. The hardware components and Arduino code are discussed, with plans to cover the software in a follow up post.

Uploaded by

carlyblack2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Sonar With Arduino, C#, JavaScript and HTML5

This document describes a project to create a sonar device using an Arduino, ultrasonic sensor, servo motor, and software components. The Arduino sketches measures distance with the ultrasonic sensor and controls the servo motor position. It sends the angle and distance data to a .NET/C# console app via serial. The app broadcasts the data to an HTML5/JavaScript client using SignalR which displays a sonar image on a canvas. The hardware components and Arduino code are discussed, with plans to cover the software in a follow up post.

Uploaded by

carlyblack2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

By Miosz Orze 16.

September 2014 00:41


<< How fast i s .NET Garbage Col l ector? Part 2. | [OoB] Sonar wi th Ardui no, C#, JavaScri pt and HTML5 (Part 2) >>
[ OoB] Sonar with Arduino, C#, JavaScript and HTML5
Thi s post marks the begi nni ng of "Out of Boredom" s eri es. I t wi l l be about creati ng s tuff wi th my recentl y purchas ed Arduino
Uno. Let's have a break from chores of professi onal programmi ng and create somethi ng just for fun :)
My fi rst Ardui no based project i s Sonar. I t uti l i zes ul trasoni c range sens or, servo, Si gnal R and canvas to create s onar i mage:
I am spl i tti ng the des cri pti on i nto two pos ts . Fi rs t post wi l l focus on hardware components and Ardui no s ketch and the
second wi l l be about .NET and JavaScript appl i cati ons. You can get compl ete code i n thi s GitHub repository. You can al so cl i ck
here to s ee short video of the whol e thi ng worki ng.
Here are hardware elements used:
Element Role
Ardui no Uno R3 Handl i ng HC-SR04, control l i ng s ervo and communi cati ng wi th PC
HC-SR04 Ul trasoni c Rangi ng Modul e Measuri ng ti me i t takes sound to bounce back from objects
9g Tower Pro Mi cro Servo Movi ng sens or to get 180 degree vi ew
Red LED Si gnal l i ng ready s tate
330 Ohm resi stor Li mi ti ng current goi ng through di ode
Breadboard and few jumper wi res Connecti ng components wi thout s ol deri ng
That's i t, jus t a few cheap components! Vi rtual l y no el ectroni cs ski l l s are requi red to compl ete thi s project. I ass ume,
however, that you have basi c Ardui no knowl edge and you know a bi t about C# and JavaScri pt.
Here i s the software stack:
Element Role
Ardui no s ketch (fi rmware) Measuri ng di stance, movi ng servo and sendi ng data to PC over s eri al port
.NET/C# 4.5 consol e appl i cati on wi th
Si gnal R l i brary
Recei vi ng data from Ardui no us i ng SerialPort cl ass and broadcasti ng angl e and
di stance i nformati on to cl i ents
HTML5 page wi th JavaScri pt Si gnal R
l i brary
Recei ves data from server and creates sonar i mage us i ng canvas el ement
Above mi ght sound a bi t overwhel mi ng but I ass ure you that the code i s short and not that compl i cated.
The basic idea goes like this: HC-SR04 sensor measures ti me i t takes an ul trasoni c s i gnal to bounce from obstacl es and thi s
gi ves as a chance to cal cul ate di s tance to these obstacl es . Posi ti on of the s ensor i s control l ed by servo. I nformati on about
di stance to objects and di recti on i n whi ch the s ensor i s poi nti ng i s sent to PC that i s runni ng cons ol e appl i cati on wi th
SignalR sever. PC recei ves the data and sends i t to JavaScri pt cl i ents that are capabl e of presenti ng sonar data i n ni ce vi s ual
way us i ng HTML5 canvas el ement...
More details!
The mai n component (except for the Ardui no of cours e) i s the HC-SR04 Ultrasonic Ranging Module. Thi s sensor works by
sendi ng sound si gnal at 40 kHz (s o above human percepti on l i mi ts ) and detecti ng the echo. That's why thi s project i s cal l ed
"Sonar" and not "Radar" - i t uses sound waves (not radi o waves ) do detect objects. The sens or shoul d work i n ranges from
2cm up to 400cm at accuracy of few mi l l i metres. But keep i n mi nd that the shape an materi al of objects mi ght affect
performance. I tes ted i t at maxi mum di s tance of about 2 meters and was happy wi th the resul ts. You can us e thi s s ensor
wi thout any l i brari es . That requi res doi ng thi ngs l i ke putti ng HIGH val ue on Trig pi n for 10uS to emi t ul tras oni c si gnal ,
meas uri ng durati on of HIGH pul se on Echo pi n and cal cul ati ng di stance knowi ng that speed of s ound i n the ai r i s around
340m/s... But there's a better way: you can use NewPing l i b (l i nk) to get the di s tance. I f you don't know how to i ncl ude new
l i brary i n your Ardui no sketch cl i ck here .
The second i mportant component i s the servo. HC-SR04 sensor has measuri ng angl e of about 15 degrees. But i f we move i t
around by attachi ng i t to s ervo's arm we can eas i l y get 180 degree vi ew. I won't get i nto detai l s on how s ervo works and
how i t i s control l ed i n thi s post. I pl an to make another post about shooti ng pai ntbal l marker wi th Ardui no+l aptop and I
wi l l des cri be i t then. For now al l you need to know i s that Ardui no comes wi th Servo l i brary whi ch makes i t very easy to
move servo i nto desi red posi ti on (angl e)... I uti l i zed 9g Tower Pro Micro Servo i n thi s project. I t's powerful enough to move
the s ensor yet can be powered di rectl y from Ardui no's +5V pi n.
Last physi cal components are LED used to si gnal the ready s tate (that i s when setup functi on had compl eted) wi th i ts
accompanyi ng resi stor. Maki ng a di ode s hi ne i s el ectroni cs equi val ent of "Hel l o Worl d!" s o I 'm s ure you know how to
handl e LED. Even i f not, you can al ways use the ti ny bui l t-i n LED connected to pi n 13 of Ardui no Uno...
Thi s di agram shows how hardware components s houl d be connected:
- about me
What for ?
I cant i magi ne worki ng as a programmer
wi thout hundreds of web pages on whi ch
peopl e "wasti ng" thei r free ti me s hare
what they managed to fi nd out. Therefore
I wi l l try to add a bi t of useful i nformati on
to the webs res ources mysel f...
Also on CodePr oject !
260K reads si nce may 2012 :)
Language
Thi s bl og i s my fi rst attempt to wri te i n
Engl i sh so i f you s ee any l anguage
mi stakes pl ease l et me know. I di dnt
have enough ti me to transl ate most of my
ol d posts but I wi l l try to make new ones
both i n Pol i sh and i n Engl i sh.
Znasz pol ski ? Kl i kni j tutaj.
Enter search term or APML url Szukaj
Include comments in search
Cat egories
.NET Framework/C# (13)
Ajax (5)
Ardui no (3)
Arti cl es (2)
ASP.NET MVC (2)
ASP.NET Web Forms (8)
Automati on (1)
CodeProject (19)
Depl oyment (1)
Ext JS (1)
Graphi cs (5)
HTML5 (3)
JavaScri pt (10)
JQuery (1)
Out Of Boredom (3)
Regex (2)
SVN (1)
Vi s ual Studi o (2)
WebSphere MQ (2)
Hist or y
2014
October (1)
September (2)
June (1)
May (1)
Apri l (1)
2013
2012
2011
2010
2009
2008
2007
Home Archive Contact Subscribe
Here's the whole code that should be uploaded to Arduino:
As stated before, I assume that you know somethi ng about Ardui no programmi ng and thi ngs l i ke const, pinMode, delay, setup
and loop don't requi re expl anati on...
Fi rst l i nes whi ch s houl d capture your attenti on are:
Above l i nes l et us us e NewPing and Servo cl asses to measure di stance and move the sensor. Noti ce al so that setup functi on
has such l i nes:
Thes e exi st to s et the pi n used to control the servo and to move the servo i nto i ni ti al posi ti on at 0 degrees.
Thi s l i ne:
al l owes Ardui no to tal k to PC (i n my case a l aptop wi th Wi ndows 7) over serial port. That's ri ght, even though Ardui no Uno i s
connected to computer vi a USB cabl e i t actual l y us es COM port to communi cate. On my machi ne i ts cal l ed "COM3" (screen
shown bel ow comes from Devi ce Manager - my Wi ndows i s i n Pol i sh):
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
#include <NewPing.h>
#include <Servo.h>

const byte setupReadyLedPin = 8;
const byte triggerPin = 10;
const byte echoPin = 11;
const byte servoPin = 12;

const byte maxDistanceInCm = 100;

byte angle;
byte angleStep;
byte angleStepDelayInMs = 50;

NewPing sonar(triggerPin, echoPin, maxDistanceInCm);
Servo servo;

void setup() {
pinMode(setupReadyLedPin, OUTPUT);

angle = 0;
angleStep = 1;

servo.attach(servoPin);
servo.write(angle);

Serial.begin(9600); // Open connection with PC

digitalWrite(setupReadyLedPin, HIGH);
}

void loop() {
alterServoMoveDirection();

measureAndSendDistance();

angle += angleStep;
servo.write(angle); // Move servo

delay(angleStepDelayInMs);
}

void alterServoMoveDirection() {
if (angle == 180) {
angleStep = -1;
} else if (angle == 0) {
angleStep = 1;
}
}

void measureAndSendDistance() {
byte distanceInCm = sonar.ping_cm(); // Use ultrasound to measure distance

byte sonarData[] = {255, angle, distanceInCm};
Serial.write(sonarData, 3); // Send data to PC
}
1
2
3
4
5
#include <NewPing.h>
#include <Servo.h>

NewPing sonar(triggerPin, echoPin, maxDistanceInCm);
Servo servo;
1
2
servo.attach(servoPin);
servo.write(angle);
1 Serial.begin(9600);
Submi t to DotNetKi cks...
Tags: Si gnal R, JavaScri pt, HTML5, canvas , C#, .NET, Ardui no, HC-SR04, servo
.NET Framework/C# | CodeProject | HTML5 | JavaScri pt | Out of Boredom | Ardui no
E-mai l *
Country
Name*
The val ue passed to begi n method determi nates baud rate (communi cati on s peed). I t's i mportant to have the s ame val ue
used i n software that communi cats wi th Ardui no.
The loop functi on moves s ervo and i nvokes measureAndSendDistance functi on whi ch uses NewPing to cal cul ate di s tance and
Serial to send data to PC. Thi s i s how eas y i t i s to get di s tance i n cm thanks to NewPing l i b:
I f meas ured di stance exceeds the maxi mum val ue speci fi ed as l as t parameter to NewPing cons tructor the val ue of 0 i s
returned. Check NewPing docs to s ee other us eful functi ons of thi s l i b.
And fi nal l y thi s i s how Ardui no sends data to PC:
The fi rs t array el ement (255) i s us ed as a marker/separator to easi l y di sti ngui s h pairs of angle-distance values. I ts rol e wi l l
become cl ear i n the s econd post whi ch wi l l des cri be Si gnal R server and cl i ents... I as sume that maxDistanceInCm cons t wi l l
never be s et above 200 so distanceInCm wi l l never have val ue of 255. 255 wi l l never be sent as an angle too becaus e our
servo moves i n 0..180 degrees range. Sure i t mi ght be a good i dea to create const and avoi d 255 magi c number. Some
val i dati on woul d be useful too... But s crew i t, thi s project i s just for fun! :)
Ok, you s urvi ved to the end of the fi rst pos t about Ardui no/.NET/JS/HTML s onar. The second pos t shoul d be ready i n about a
week.
Update 2014-09-29: Here's the Part 2 .
Permal i nk | Comments (0)
Add comment


United States

Noti fy me when new comments are added
Save comment
Powered by BlogEngine.NET
Theme by Mads Kristensen (with some modifications!)
1 byte distanceInCm = sonar.ping_cm()
1
2
byte sonarData[] = {255, angle, distanceInCm};
Serial.write(sonarData, 3); // Send data to PC
b i u quote
Comment Previ ew

You might also like