You are on page 1of 3


HC-SR04 Ping distance sensor]

VCC to arduino 5v GND to arduino GND
Eco to !rduino "in #$ %rig to !rduino "in #&
Red P'S to !rduino "in ##
Green P'S to !rduino "in #0
5(0 o) resistor to *ot +ED NEG and GRD "o,er rai-
.ore in/o at0 tt"0//goo1g-/234G-
'rigina- code i)"rove)ents to te Ping s2etc sourced /ro) %ro--)a2er1co)
So)e code and ,iring ins"ired *5
7de/ine trigPin #$
7de/ine ecoPin #&
7de/ine -ed ##
7de/ine -ed& #0
void setu"89 :
Seria-1*egin 8;(009<
"in.ode8trigPin= '6%P6%9<
"in.ode8ecoPin= >NP6%9<
"in.ode8-ed= '6%P6%9<
"in.ode8-ed&= '6%P6%9<
void -oo"89 :
-ong duration= distance<
digita-@rite8trigPin= +'@9< // !dded tis -ine
de-a5.icroseconds8&9< // !dded tis -ine
digita-@rite8trigPin= H>GH9<
// de-a5.icroseconds8#0009< - Re)oved tis -ine
de-a5.icroseconds8#09< // !dded tis -ine
digita-@rite8trigPin= +'@9<
duration A "u-se>n8ecoPin= H>GH9<
distance A 8duration/&9 / &;1#<
i/ 8distance B 49 : // %is is ,ere te +ED 'n/'// a""ens
digita-@rite8-ed=H>GH9< // @en te Red condition is )et= te Green +ED
sou-d turn o//
e-se :
i/ 8distance CA &00 DD distance BA 09:
Seria-1"rint-n8E'ut o/ rangeE9<
e-se :
Seria-1"rint-n8E c)E9<
DC motors are used for all sort of robotic projects.
The motor shield can drive up to 4 DC motors bi-directionally. That means they can
be driven forwards and backwards. The speed can also be varied at 0.5 increments
usin! the hi!h-"uality built in #$%. This means the speed is very smooth and won&t
(ote that the )-brid!e chip is not meant for drivin! loads over 0.*+ or that peak over
,.-+ so this is for small motors. Check the datasheet for information about the motor
to verify its ./.
To connect a motor0 simply solder two wires to the terminals and then connect them
to either the M1, M2, M3, or M4. Then follow these steps in your sketch
,. %ake sure you 1include 2+3%otor.h4
-. Create the +35DC%otor object withAF_DCMotor(motor#, frequency)0 to
setup the motor )-brid!e and latches. The constructor takes two ar!uments.
The 6rst is which port the motor is connected to0 1, 2, 3or 4.
frequency is how fast the speed controllin! si!nal is.
3or motors , and - you can
choose MOTOR12_64KHZ0MOTOR12_8KHZ0 MOTOR12_2KHZ0
orMOTOR12_1KHZ. + hi!h speed like *4/)7 wont be audible but a low speed
like ,/)7 will use less power. %otors 8 9 4 are only possible to run at ,/)7
and will i!nore any settin! !iven
8. Then you can set the speed of the motor usin!setSpeed(speed) where
the speed ran!es from 0 :stopped; to -55 :full speed;. <ou can set the speed
whenever you want.
4. To run the motor0
call ru(direction) where direction isFOR!ARD0 "ACK!ARD or R#$#AS#
. .f course0 the +rduino doesn&t actually know if the motor is &forward& or
&backward&0 so if you want to chan!e which way it thinks is forward0 simply
swap the two wires from the motor to the shield.
Copy Code
,. 1include 2+3%otor.h4
8. +35DC%otor motor:-0 %.T.=,-5*4/)>;? @@ create motor 1-0 *4/)7 pwm
5. void setup:; A
*.!in:C*00;? @@ set up Berial library at C*00 bps
D. Berial.println:E%otor test'E;?
C. motor.setBpeed:-00;? @@ set the speed to -00@-55
,0. G
,-. void loop:; A
,8. Berial.print:EtickE;?
,5.$+=D;? @@ turn it on !oin! forward
,*. delay:,000;?
,F. Berial.print:EtockE;?
,C.$+=D;? @@ the other way
-0. delay:,000;?
--. Berial.print:EtackE;?
-8.;? @@ stopped
-4. delay:,000;?
-5. G