Instrucciones Montaje

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 75

Category Archives: Arduino

ARDUINO, VIDEO

BUILDING A 4WD AUTONOMOUS


CAR WITH ARDUINO
MAY 6, 2016

In this tutorial you will learn to make a 4WD robotic car with built-in obstacle
avoidance. This project involves building a 4WD car with an ultrasonic sensor that can
detect nearby objects and change its direction to avoid these objects. The ultrasonic
sensor will be attached to a servo motor which is constantly scanning left and right
looking for objects in its way.

4WD
autonomous robotic car

You can extend this project by adding more sensors such as ultrasonic sensor to the rear
so that the car doesn’t reverse into objects, speed detection sensors to determine speed,
line following sensors, and LED brake lights. You could even add light sensors and
LEDs to turn headlights on in low-light conditions or a Bluetooth connection to control
the car through a mobile app.
Parts required
Here is what you will need:

 1x Arduino Uno board


 1x 4WD Robotic Car kit (4 wheels, 4 motors, chassis, AA battery enclosure, screws)
 6x AA batteries
 1x 9V battery
 1x 9V battery power cable barrel jack connector
 1x L298N Motor Module
 1x Arduino Sensor Shield v5.0
 1x HC-SR04 Ultrasonic Sensor
 1x Servo motor (any small servo motor will do but if you don’t have one you can leave it
out)
 Assorted colour wires:
 4x female-to-female wires for Ultrasonic Sensor
 8x female-to-female wires for the Motor Module to Sensor Shield
 8x bare-ended wires to go from the four motors to the Motor Module
 2x bare-ended wires to go from the Motor Module to Sensor Shield
 3x wires for the servo motor with female end attaching to Sensor Shield (should already
be included with the servo motor)
 2x bare-ended wires to go from AA Battery enclosure to the motor Module (should
already be included with the battery enclosure)
 Arduino IDE software – free at http://www.arduino.cc
 1x USB cable (A male plug to B male plug)
 A computer to program with
Note: Make sure you get a range of different colours for the wires (eg. red for voltage,
black for ground, etc.). Some items such as the battery enclosure, motors and servo
motor may come with wires so you may not need to purchase these separately. Some
motor modules or sensor shields may use different pins (eg. male or female or just bare-
ended wires).
Below are images of the main components you need (apart from the wiring and
batteries) – the different robot parts, shields, modules and sensors.
Click for larger image

Assembling the robotic car


The first thing to do is to assemble all the car chassis parts and attach the motors to the
wheels. This is explained in detail in the video below.

Video Player

00:00

15:59

Wiring it up
Now that the chassis is assembled and the wheels and motors are attached, you will
need to wire up all of the components. You can watch the video below or scroll down
for the step-by-step instructions and wiring diagram.

Video Player

00:00

18:38

1. Connect the red wire (voltage) from the battery enclosure to VCC on the L298N Motor
Module.
2. Connect the black wire (ground) from the battery enclosure to GND on the Motor Module.
3. Connect the red wire from Motor A1 (rear left) and the red wire from Motor A2 (front left)
to OUT1 on the Motor Module (both wires can be twisted together and will go into OUT1 –
the wiring diagram further down this page shows how to do this).
4. Connect the black wire from Motor A1 (rear left) and the black wire from Motor A2 (front
left) to OUT2 on the Motor Module.
5. Connect the red wire from Motor B1 (rear right) and the red wire from Motor B2 (front
right) to OUT3 on the Motor Module.
6. Connect the black wire from Motor B1 (rear right) and the black wire from Motor B2 (front
right) to OUT4 on the Motor Module.
7. Connect a female-to-female end wire from ENA (Engine A) on the Motor Module to signal
pin 1 on the Sensor Shield (the #2 pin in the S row. There are three rows – S stands for
Signal, V for Voltage and G for Ground).
8. Connect a female-to-female end wire from 5V (next to ENA) on the Motor Module to
voltage pin 1 on the Sensor Shield (it doesn’t really matter which pin for voltage as long as it
is in the V row).
9. Connect a female-to-female end wire from IN1 on the Motor Module to signal pin 2 on the
Sensor Shield.
10. Connect a female-to-female end wire from IN2 on the Motor Module to signal pin 3 on the
Sensor Shield.
11. Connect a female-to-female end wire from IN3 on the Motor Module to signal pin 4 on the
Sensor Shield.
12. Connect a female-to-female end wire from IN4 on the Motor Module to signal pin 5 on the
Sensor Shield.
13. Connect a female-to-female end wire from 5V (next to ENB) on the Motor Module to
voltage pin 6 on the Sensor Shield (it doesn’t really matter which pin for voltage as long as it
is in the V row).
14. Connect a female-to-female end wire from ENB (Engine B) on the Motor Module to signal
pin 6 on the Sensor Shield.
15. Connect the signal wire from the servo motor to signal (S) pin 7 on the Sensor Shield, the
voltage wire from the servo motor to the voltage (V) pin 7 on the Sensor Shield, and the
ground wire from the servo motor to the GND (G) pin 7 on the Sensor Shield.
16. Attach the Ultrasonic Sensor to the servo motor and then attach the servo motor to the front
of the car so that it can scan left and right looking for objects in its path.
You can use sticky tape, blu-tack, glue or a 3D printed/laser cut mount or whatever you like
to attach everything, as long as everything is firmly mounted.
17. Use female-to-female end plugs to connect the Ultrasonic sensor to the Sensor Shield.
Firstly, attach the TRIG pin from the Ultrasonic sensor to signal (S) pin 8 on the Sensor
Shield. Attach the ECHO pin from the ultrasonic sensor to signal (S) pin 9 on the Sensor
Shield. Attach the VCC pin from the Ultrasonic Sensor to the voltage (V) pin 10 on the
Sensor Shield. Lastly, attach the GND pin from the Ultrasonic Sensor to the GND (G) pin 11
on the Sensor Shield.
18. Now attach the Sensor Shield on top of the Arduino Uno board making sure to not bend any
pins. Connect a 9V Battery Barrel Jack Connector to a 9V battery and then plug this in to the
Arduino’s barrel jack (see diagram below).
19. That’s it! Now all you need is batteries and the code!
Please note: There are several variations of the sensors and modules and several
different ways to wire this up. For example, you can add a charging component to
charge batteries and you can also add different switches. There may be other better
methods to use, but I have gone for the simplest way I know of to set this up.
Wiring diagram
Click for larger image

Writing the code


Here is the code to get your robot moving. The code is written in C language and is
written in the Arduino IDE software. The code for a robot with a servo motor and the
code for a robot without a servo motor is shown below. If you don’t have an ultrasonic
sensor and just want to use Bluetooth control via a phone app, then you can just extract
the parts of the code that you need and then add the Bluetooth code.

Code to use with a servo motor


1. // Setup the servo motor
2. #include <Servo.h>
3. Servo myservo;
4. int servposnum = 0;
5. int servpos = 0;
6.
7. // Setup Motor A (front and rear) pins
8. int enableA = 1;
9. int pinA1 = 3;
10. int pinA2 = 2;
11.
12. // Setup Motor B (front and rear) pins
13. int enableB = 6;
14. int pinB1 = 5;
15. int pinB2 = 4;
16.
17. // Setup Ultrasonic Sensor pins
18. #define trigPin 8
19. #define echoPin 9
20.
21.
22. void setup() {
23. // The setup code goes here and runs once only
24. // Configure the pin modes for each drive motor
25. pinMode (enableA, OUTPUT);
26. pinMode (pinA1, OUTPUT);
27. pinMode (pinA2, OUTPUT);
28.
29. pinMode (enableB, OUTPUT);
30. pinMode (pinB1, OUTPUT);
31. pinMode (pinB2, OUTPUT);
32.
33. // Configure the pin modes for the Ultrasonic Sensor
34. pinMode(trigPin, OUTPUT);
35. pinMode(echoPin, INPUT);
36.
37. // Turn pin into servo driver. Calls pinMode. Returns 0 on failure.
38. myservo.attach(7);
39. }
40.
41. void loop() {
42. // Main code goes here and will run repeatedly:
43.
44. car(); // function keeps moving car forward while distance > 15cm
45. avoid(); // function makes car go back, turn slightly right to move forward in new direction
46.
47. }
48.
49. // Create motor functions
50. void motorAforward() {
51. digitalWrite (pinA1, HIGH);
52. digitalWrite (pinA2, LOW);
53. }
54. void motorBforward() {
55. digitalWrite (pinB1, LOW);
56. digitalWrite (pinB2, HIGH);
57. }
58. void motorAbackward() {
59. digitalWrite (pinA1, LOW);
60. digitalWrite (pinA2, HIGH);
61. }
62. void motorBbackward() {
63. digitalWrite (pinB1, HIGH);
64. digitalWrite (pinB2, LOW);
65. }
66. void motorAstop() {
67. digitalWrite (pinA1, HIGH);
68. digitalWrite (pinA2, HIGH);
69. }
70. void motorBstop() {
71. digitalWrite (pinB1, HIGH);
72. digitalWrite (pinB2, HIGH);
73. }
74. void motorAcoast() {
75. digitalWrite (pinA1, LOW);
76. digitalWrite (pinA2, LOW);
77. }
78. void motorBcoast() {
79. digitalWrite (pinB1, LOW);
80. digitalWrite (pinB2, LOW);
81. }
82. void motorAon() {
83. digitalWrite (enableA, HIGH);
84. }
85. void motorBon() {
86. digitalWrite (enableB, HIGH);
87. }
88. void motorAoff() {
89. digitalWrite (enableA, LOW);
90. }
91. void motorBoff() {
92. digitalWrite (enableB, LOW);
93. }
94.
95. // Setup movement functions
96. void forward (int duration) {
97. motorAforward();
98. motorBforward();
99. delay (duration);
100. }
101. void backward (int duration) {
102. motorAbackward();
103. motorBbackward();
104. delay (duration);
105. }
106. void right (int duration) {
107. motorAbackward();
108. motorBforward();
109. delay (duration);
110. }
111. void left (int duration) {
112. motorAforward();
113. motorBbackward();
114. delay (duration);
115. }
116. void coast (int duration) {
117. motorAcoast();
118. motorBcoast();
119. delay (duration);
120. }
121. void breakRobot (int duration) {
122. motorAstop();
123. motorBstop();
124. delay (duration);
125. }
126. void disableMotors() {
127. motorAoff();
128. motorBoff();
129. }
130. void enableMotors() {
131. motorAon();
132. motorBon();
133. }
134.
135. // Setup Ultrasonic Sensor distance measuring
136. int distance() {
137. int duration, distance;
138. digitalWrite(trigPin, HIGH);
139. delayMicroseconds(1000);
140. digitalWrite(trigPin, LOW);
141. duration = pulseIn(echoPin, HIGH);
142. distance = (duration/2) / 29.1;
143. return distance;
144. }
145.
146. // Setup the main car function
147. void car() {
148. int distance_0;
149. distance_0 = distance();
150. // Keep moving forward in a straight line while distance of objects > 15cm
151. while(distance_0 > 15)
152. {
153. // Keep moving servo motor back and forth to scan surroundings
154. // This allows the ultrasonic sensor to see more to its left and right
155. if(servposnum == 0)
156. {
157. myservo.writeMicroseconds (1900);
158. servposnum = 1;
159. delay(100);
160. }
161. else if(servposnum == 1)
162. {
163. myservo.writeMicroseconds (2200);
164. servposnum = 2;
165. delay(100);
166. }
167. else if(servposnum == 2)
168. {
169. myservo.writeMicroseconds (1900);
170. servposnum = 3;
171. delay(100);
172. }
173. else if(servposnum == 3)
174. {
175. myservo.writeMicroseconds (1600);
176. servposnum = 1;
177. delay(100);
178. }
179. motorAon();
180. motorBon();
181. forward(1);
182. distance_0 = distance();
183.
184. }
185. breakRobot(0);
186.
187. }
188. void avoid()
189. {
190. // Go back and turn slightly right to move car in new direction if object detected < 15cm away
191. backward(500);
192. right(360);
193. }

Code to use without a servo motor


1. // Setup Motor A (front and rear) pins
2. int enableA = 1;
3. int pinA1 = 3;
4. int pinA2 = 2;
5.
6. // Setup Motor B (front and rear) pins
7. int enableB = 6;
8. int pinB1 = 5;
9. int pinB2 = 4;
10.
11. // Setup Ultrasonic Sensor pins
12. #define trigPin 8
13. #define echoPin 9
14.
15. void setup() {
16. // The setup code goes here and runs once only
17. // Configure the pin modes for each drive motor
18. pinMode (enableA, OUTPUT);
19. pinMode (pinA1, OUTPUT);
20. pinMode (pinA2, OUTPUT);
21.
22. pinMode (enableB, OUTPUT);
23. pinMode (pinB1, OUTPUT);
24. pinMode (pinB2, OUTPUT);
25.
26. // Configure the pin modes for the Ultrasonic Sensor
27. pinMode(trigPin, OUTPUT);
28. pinMode(echoPin, INPUT);
29. }
30.
31. void loop() {
32. // Main code goes here and will run repeatedly:
33. car(); // function keeps moving car forward while distance of objects in front are > 15cm away
34. avoid(); // function makes car go back, turn slightly right to move forward in new direction
35. }
36.
37. // Create motor functions
38. void motorAforward() {
39. digitalWrite (pinA1, HIGH);
40. digitalWrite (pinA2, LOW);
41. }
42. void motorBforward() {
43. digitalWrite (pinB1, LOW);
44. digitalWrite (pinB2, HIGH);
45. }
46. void motorAbackward() {
47. digitalWrite (pinA1, LOW);
48. digitalWrite (pinA2, HIGH);
49. }
50. void motorBbackward() {
51. digitalWrite (pinB1, HIGH);
52. digitalWrite (pinB2, LOW);
53. }
54. void motorAstop() {
55. digitalWrite (pinA1, HIGH);
56. digitalWrite (pinA2, HIGH);
57. }
58. void motorBstop() {
59. digitalWrite (pinB1, HIGH);
60. digitalWrite (pinB2, HIGH);
61. }
62. void motorAcoast() {
63. digitalWrite (pinA1, LOW);
64. digitalWrite (pinA2, LOW);
65. }
66. void motorBcoast() {
67. digitalWrite (pinB1, LOW);
68. digitalWrite (pinB2, LOW);
69. }
70. void motorAon() {
71. digitalWrite (enableA, HIGH);
72. }
73. void motorBon() {
74. digitalWrite (enableB, HIGH);
75. }
76. void motorAoff() {
77. digitalWrite (enableA, LOW);
78. }
79. void motorBoff() {
80. digitalWrite (enableB, LOW);
81. }
82.
83. // Setup movement functions
84. void forward (int duration) {
85. motorAforward();
86. motorBforward();
87. delay (duration);
88. }
89. void backward (int duration) {
90. motorAbackward();
91. motorBbackward();
92. delay (duration);
93. }
94. void right (int duration) {
95. motorAbackward();
96. motorBforward();
97. delay (duration);
98. }
99. void left (int duration) {
100. motorAforward();
101. motorBbackward();
102. delay (duration);
103. }
104. void coast (int duration) {
105. motorAcoast();
106. motorBcoast();
107. delay (duration);
108. }
109. void breakRobot (int duration) {
110. motorAstop();
111. motorBstop();
112. delay (duration);
113. }
114. void disableMotors() {
115. motorAoff();
116. motorBoff();
117. }
118. void enableMotors() {
119. motorAon();
120. motorBon();
121. }
122.
123. // Setup Ultrasonic Sensor distance measuring
124. int distance() {
125. int duration, distance;
126. digitalWrite(trigPin, HIGH);
127. delayMicroseconds(1000);
128. digitalWrite(trigPin, LOW);
129. duration = pulseIn(echoPin, HIGH);
130. distance = (duration/2) / 29.1;
131. return distance;
132. }
133.
134. // Setup the main car function
135. void car() {
136. int distance_0;
137. distance_0 = distance();
138. // Keep moving forward in a straight line while distance of objects in front > 15cm away
139. while(distance_0 > 15)
140. {
141. motorAon();
142. motorBon();
143. forward(1);
144. distance_0 = distance();
145. }
146. breakRobot(0);
147.
148. }
149.
150. // Go back and turn slightly right to move car in new direction
151. // This function only runs if an obstacle within 15cm is detected
152. void avoid()
153. {
154. backward(500);
155. right(360);
156. }

Finishing up
Once you have the code you will need to verify it and then upload it to your Arduino
Uno board. Make sure you have either the Arduino disconnected from the Sensor
Shield or all batteries disconnected when uploading the code to prevent any errors
while uploading.Make sure to also arrange the different components carefully in the
chassis to prevent any short circuits.
And that’s it! There is so much more that can be done with this project. Try extending
your project by adding the following features:
 Line following
 Low-light activated headlights (using light sensors and LEDs)
 Brake lights
 Speed measuring
 Bluetooth/mobile app control
 More ultrasonic sensors to avoid reversing into objects or falling off edges
The video below shows a demo of the 4WD autonomous car using an Arduino and the
ultrasonic sensor attache to a servo motor. Six AA batteries power the motors while the
Arduino board is powered using a separate 9V battery.

Video Player

00:00

00:21
More photos
4WDARDUINOAUTONOMOUSCCARCODELANGUAGEPROJECTROBOTROBOTICSELF-
DRIVINGTUTORIALVIDEO

ARDUINO, VIDEO

BUILDING A 2WD ARDUINO


ROBOTIC CAR
SEPTEMBER 1, 2015

This tutorial will show you how to use an Arduino to build a 2WD robotic car. You can
extend this project by using sensors to measure wheel speed (for improved accuracy
when steering or maintaining a straight line) or sensors to avoid objects or follow a line.

Parts required
Here is what you will need:

 Arduino Uno board


 2WD robotic car kit (2 wheels, 2 motors, chassis)
 4 x AA batteries
 Motor shield with 6 wires
 Arduino IDE software (free at http://www.arduino.cc/)
 USB cable
 2 x wheel speed encoders and infrared sensors (optional)
Assembling the robot
 

1. Assemble all chassis parts and attach the motors to the wheels (if not already assembled).
2. Connect the red wire from the battery enclosure to VIN on the Motor Shield.
3. Connect the black wire from the battery enclosure to GND on the Motor Shield.
4. Connect the red wire from Motor A to A+ on the Motor Shield, and the black wire from
Motor A to A- on the Motor Shield.
5. Connect the red wire from Motor B to B+ on the Motor Shield, and the black wire from
Motor B to B- on the Motor Shield.
6. Attach the Motor Shield to the top of the Arduino Uno board – be careful not to bend or
damage any of the pins!
7. Connect the Arduino Uno board to the computer via the USB cable and upload the code
using the Arduino IDE software.
8. Insert four AA batteries in the battery enclosure.
9. Test your robot! If your robot goes in the wrong direction or doesn’t move, check that you
have the Motor pins in the correct places and make sure no pins are loose.
Extra challenges:
 Control your robotic car using an Android app on a smartphone via Bluetooth (refer to the
Controlling an LED by Bluetooth tutorial for instructions on how to make an app using App
Inventor and how to attach a Bluetooth module).
 Add code that will control the speed of the motors using the encoder discs and infrared
sensors.
The code to test your robot
Use this code to test that your wheels spin and that your robot is wired up correctly.

void setup() {
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel B pin
pinMode(8, OUTPUT); //Initiates Brake Channel B pin
}
void loop(){
//Motor A forward @ full speed
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 123); //Spins the motor on Channel B at half speed
delay(3000);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(8, HIGH); //Engage the Brake for Channel B
delay(1000);
//Motor A forward @ full speed
digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 123); //Spins the motor on Channel A at half speed
//Motor B forward @ full speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
delay(3000);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(8, HIGH); //Engage the Brake for Channel B
delay(1000);
}
Click here if you would like a PDF copy of these instructions.
2WDARDUINOBUILDBUILDINGCARCODEIDEPROJECTROBOTROBOTICUNO

ARDUINO, VIDEO
ARDUINO WEATHER STATION
PROJECT
SEPTEMBER 1, 2015

This tutorial will show you how to take temperature readings using the Freetronics
temperature sensor and an Arduino Uno.
Note that there are two different module types: the temperature sensor module and the
humidity & temperature sensor module. If you have the humidity & temperature sensor
module you will need to download a library for the Arduino IDE and use different
wiring and code – follow the instructions for that module
here: http://www.freetronics.com.au/pages/humid-humidity-temperature-sensor-
module-quickstart-guide
Parts required
Here is what you will need:

 Arduino Uno board


 Temperature Sensor Module
 Breadboard
 3 male-to-male jumper wires
 Arduino IDE software (free at http://www.arduino.cc/)
 USB cable
Wire it up!

Attach the temperature sensor module to your breadboard. The GND pin should connect
the GND pin on the Arduino board, the VCC pin should connect the 5V pin on the
Arduino board, and the DATA pin should connect to the digital I/O pin 2 on the
Arduino board – all these are connected via the breadboard using male-to-male jumper
wires.

The pictures below show how to wire it all up in greater detail.


 

Arduino code
Here is the code for the Arduino sketch (you can also copy it
from http://www.freetronics.com.au/pages/temp-temperature-sensor-module-quickstart-
guide). Make sure you scroll down to see how to test the sensor.
#define REF_PIN 2
void getCurrentTemp( int *sign, int *whole, int *fract);
char temp_string[10];
void setup(){
Serial.begin(9600);
// initialize DS18B20 datapin
digitalWrite(REF_PIN, LOW);
pinMode(REF_PIN, INPUT); // sets the digital pin as input (logic 1)
pinMode(15, INPUT);
}
void loop(){
getCurrentTemp(temp_string);
Serial.println(temp_string);
delay(1000);
}
void OneWireReset (int Pin) // reset. Should improve to act as a presence pulse
{
digitalWrite (Pin, LOW);
pinMode (Pin, OUTPUT); // bring low for 500 us
delayMicroseconds (500);
pinMode (Pin, INPUT);
delayMicroseconds (500);
}
void OneWireOutByte (int Pin, byte d) // output byte d (least sig bit first).
{
byte n;

for (n=8; n!=0; n--)


{
if ((d & 0x01) == 1) // test least sig bit
{
digitalWrite (Pin, LOW);
pinMode (Pin, OUTPUT);
delayMicroseconds (5);
pinMode (Pin, INPUT);
delayMicroseconds (60);
}
else
{
digitalWrite (Pin, LOW);
pinMode (Pin, OUTPUT);
delayMicroseconds (60);
pinMode (Pin, INPUT);
}
d = d>>1; // now the next bit is in the least sig bit position.
}
}
byte OneWireInByte (int Pin) // read byte, least sig byte first
{
byte d, n, b;

for (n=0; n<8; n++) { digitalWrite (Pin, LOW); pinMode (Pin, OUTPUT); delayM
delayMicroseconds (50); d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
}
return (d);
}
void getCurrentTemp (char *temp)
{
int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;
OneWireReset (REF_PIN);
OneWireOutByte (REF_PIN, 0xcc);
OneWireOutByte (REF_PIN, 0x44); // perform temperature conversion, strong pullup for one sec
OneWireReset (REF_PIN);
OneWireOutByte (REF_PIN, 0xcc);
OneWireOutByte (REF_PIN, 0xbe);
LowByte = OneWireInByte (REF_PIN);
HighByte = OneWireInByte (REF_PIN);
TReading = (HighByte << 8) + LowByte;
sign = TReading & 0x8000; // test most sig bit
if (sign) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25

whole = Tc_100 / 100; // separate off the whole and fractional portions
fract = Tc_100 % 100;

if (sign) {
temp[0] = '-';
} else {
temp[0] = '+';
}

if (whole/100 == 0) {
temp[1] = ' ';
} else {
temp[1] = whole/100+'0';
}
temp[2] = (whole-(whole/100)*100)/10 +'0' ;
temp[3] = whole-(whole/10)*10 +'0';
temp[4] = '.';
temp[5] = fract/10 +'0';
temp[6] = fract-(fract/10)*10 +'0';
temp[7] = '\0';
}

Testing
Upload the code once you connect the Arduino to the computer with a USB cable. In
the Arduino IDE, click on Tools > Serial Monitor. Make sure you change the baud rate
to 9600 baud. The temperature sensor should have a constant blue light and the Serial
Monitor should display the temperature readings every second.
Think about how you could use the temperature sensor. You can connect the Arduino
to a dot-matrix or LCD display to display the temperature. It could also be used for
climate control inside a room. How else could you use it?
Click here if you would like to download a PDF copy of these instructions.
ARDUINOCODEIDEPROJECTSENSORSTATIONTEMPERATURETUTORIALUNOWEATHER

ARDUINO, VIDEO
CONTROLLING AN LED USING AN
ANDROID PHONE VIA BLUETOOTH
SEPTEMBER 1, 2015

This tutorial will show you how to make an Android app that can control an LED on an
Arduino using a Bluetooth wireless connection.

Parts required
Here is what you will need:

 Phone with Android OS and Bluetooth


 Arduino Uno board
 Breadboard
 4 male-to-female jumper wires
 2 male-to-male jumper wires
 470 Ohm resistor
 BC417 Bluetooth module (HC-06)
 App Inventor software (free at http://ai2.appinventor.mit.edu/)
 Arduino IDE software (free at http://www.arduino.cc/)
The first part of this tutorial shows how to wire up the Arduino, LED, and Bluetooth
module to a breadboard. Then we will look at the code you will need for the Arduino
sketch. The second part of the tutorial shows how to build the Android app using App
Inventor.

You must pair the Bluetooth device with your Android phone in the Bluetooth settings
on the phone. The password for pairing is usually 1234. Then in the app you have made,
you will need to connect to the Bluetooth device. NOTE: It is important to remove the
Bluetooth module from the Arduino while the sketch is uploaded to the Arduino board.
Wire it up!
Rx on Bluetooth module connects to Tx on Arduino board. Tx on Bluetooth module
connects to Rx on Arduino board. GND on Bluetooth module connects to GND on
Arduino board and the VCC wire on the Bluetooth module connects to 3.3V on Arduino
board. The LED can be connected to a breadboard with a 470 Ohm resistor. The LED
should be connected to Pin 13 and GND on Arduino.

The following images show you how to wire it up.


 

Arduino code
Here is the code for the Arduino sketch:

int ledPin = 13; // LED light is on PIN 13


String readString;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
while ( Serial.available() )
{ // While there is data in the buffer
delay( 3 );
char c = Serial.read(); // reads characters sent from the Android app to Bluetooth module
readString += c; // build the string - “on” or “off”
}
if ( readString.length() >0 ) // only reads on/off if string length is more than 0
{
Serial.println( readString );
if ( readString == "on" ) // if string is on, then turn LED on
{
digitalWrite(ledPin, HIGH) ;
}
if ( readString == "off" ) // if string is off, then turn LED off
{
digitalWrite(ledPin, LOW );
}
readString=""; // sets string to empty so that it has length of 0 and maintains current light state until string is m
}
}

App design
Here is the basic screen design of the Android app on App Inventor but you can create
your own interface with more graphics and larger buttons.
 

App Inventor code blocks


Here is the App Inventor code that you will need:
Click on the image for a larger version.

That’s it! Now you just need to click on Build > App (Save .apk to my computer) and
send the app to an Android phone. Install the app and upload the Arduino code to the
Arduino board, making sure all wires are set up correctly. Make sure you disconnect the
Bluetooth module from 3.3V before uploading the Arduino code to the board as it can
cause some issues during the upload.
Click here to download a printable PDF copy of these instructions.
ANDROIDAPPARDUINOBLUETOOTHCONTROLCONTROLLINGIDELEDLIGHTMOBILEPHONETUTORIA
LUNO

ARDUINO, VIDEO

CHRISTMAS LIGHTS ARDUINO


PROJECT
MAY 3, 2015

The project
This project involves green and red LED lights which flash in an alternating pattern. In
this project, you will use two LEDs but you can add as many as you want if you have
enough jumper wires and LEDs.

Scroll down to view the instructions or click here to download a PDF copy of the
instructions.
What you need:
 1 x Arduino Uno or compatible board
 1 x red LED
 1 x green LED
 2 x 470 Ohm resistor (yellow-violet-black-black-brown, or yellow-violet-brown-gold)
 1 x Solderless breadboard
 3 x jumper wires
Photos
Wiring it up

The LEDs are connected to pins


12 and 13 and each one has a 470 Ohm resistor connect to GND via the breadboard.

The code
Here is the Arduino sketch code:

/*
Christmas lights - blinking red and green LEDs
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int redLED = 13;
int greenLED = 12;

// the setup routine runs once when you press reset:


void setup() {
// initialize the digital pin as an output.
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
}

// the loop routine runs over and over again forever:


void loop() {
digitalWrite(redLED, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(greenLED, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
digitalWrite(redLED, LOW); // turn the LED off by making the voltage LOW
digitalWrite(greenLED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
}
Use the Arduino IDE to write this code, verify it and then upload it to the Arduino
board. For an extra challenge, why not add a light sensor so that the lights only come on
when it’s dark, or a button so that the lights can be switched on and off easily? You can
also use a chain of LEDs instead of separately wiring them up to a breadboard.

ARDUINOCHRISTMASCODECODINGLEDLIGHTSSKETCHTUTORIAL

ARDUINO, VIDEO

MAKING AN LED SENSOR NIGHT


LIGHT WITH ARDUINO
DECEMBER 14, 2014 LEAVE A COMMENT

This video tutorial explains how to use an Arduino Uno to make an LED night light.
The project involves an Arduino Uno board, an LED, a light sensor which can detect
different levels of light and return a value, a breadboard with jumper wires and resistor.
Basically the way the project works is as follows. Use the light sensor to get light
readings constantly. You can specify a value which, when the light is less than this
value an LED light will be turned on, and when the light is greater than this value the
LED light will be turned off.

This project allows you to work with getting readings from a sensor (input), use if
statements to test conditions, and use an LED to produce some form of output.

Watch the video below or click here to view it on YouTube.


Video Player

00:00
09:53

Downloads:
 LED Night Light Printed instructions, code and schematic (pdf)
 LED Night Light actual Arduino INO file (.INO file download)
The source code is also available here:

int lightLevel;
int led = 13;
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(38400); // Go to Tools > Serial Monitor and change to 38400 bauds to see light value readings
}

// this loop will turn the light on when light reading is below value of 10
// there is a delay of 1 second between checking the light value
void loop()
{
lightLevel = analogRead(A0);
Serial.println(lightLevel, DEC); // this shows the light level reading in Arduino IDE (go to Tools > Serial Mon

if (lightLevel < 50) // change this value to suit light conditions. Ranges from 0 (very dark) to 1023 (very bright).
{
digitalWrite(led, HIGH); // turns the light on
}
else
{
digitalWrite(led, LOW); // turns the light off
}
delay(1000);
}
Photos
Light sensor readings

ARDUINOAUTOMATICBOARDCODINGGUIDEHOWTOLEDLIGHTNIGHTPROGRAMMINGPROJECTSEN
SORTUTORIALUNOVIDEO

ARDUINO, VIDEO

BLINKING LED ARDUINO PROJECT


DECEMBER 12, 2014 LEAVE A COMMENT

This video tutorial has been designed for those just starting out with Arduino. For this
beginner project you will need an Arduino Unoboard (or compatible board), an LED of
any colour, a 470-Ohm resistor, two jumper wires, a breadboard, a male A-B USB
cable, and a computer (Mac/Windows) with the Arduino IDE softwareinstalled.
This project involves programming your Arduino Uno to make an LED light blink on
and off. To get started, watch the video below or click here to view it on YouTube.
Access the links below to get a copy of the code, printed instructions, schematic, and a
guide to resistors.
Video Player

00:00

17:06

Downloads:
 Blinking LED printed instructions, code and schematic (pdf)
 Blinking LED actual Arduino INO file (.INO file download)
The source code is also available here:

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
*/

// Give the LED a name - it will be connected on PIN 13


int led = 13;

// the setup routine runs once when you press reset:


void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second (1000 = 1 sec.)
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Photos
ARDUINOBEGINNERBLINKINGBREADBOARDCCODINGFIRSTGUIDEHOWTOIDELANGUAGELEDLIGH
TPINSPROGRAMMINGPROJECTRESISTORSTUTORIALVIDEOWIRING

TAGS

code coding colour conditional csharpcss 
2d 3d add adobe android animation app arraysc 

css3 data design developmenteffect flash game guide html html5 illustratorimage input j

avascript maya mobile objects phonephp player programming source statementsstyle text tu
torial unity video webxamarin
Learn to code today!

RECENT POSTS

 Creating an emoji chase game with WoofJS


 Coding an apple catcher game with WoofJS
 Creating your first 2D game in WoofJS
 Methods in C#
 Foreach loops in C#
 Arrays in C#
 Do while loops in C#

So you want to make a smart car? Don't know where to start? How about
learning with ICStation team.

The Arduino Bluetooth Smart Car has the function of Complete tracking,


obstacle avoidance, infrared remote control and Bluetooth remote control. It is a
MCU study and application development system base on Atmege328.The
greatest feature is that you can control it with Mobile phone bluetooth of Android
system. That’s may have a lot of fun.To make it, you need a number of kits
which contain a number of interesting programs, and expand external circuit
module to implement functions.
So now let's begin to build this smart car.

Major Parameter:
  1>.Motor parameter:
        Power apply:6V-9V;
        Reduction gear ratio:1:48;
  2>.Selection L298N module driver motor,segregate form CPU.
  3>.Three tracking modules,detection white and black line.Available with
fall prevention control.
  4>.Infrared remote communication module,composition of remote control
system of smart car.
  5>.Bluetooth wireless module,can match with mobile phone to control smart
car.
  6>.Input voltage 7V-12V,can connect to different kinds of sensor.The more
sensors ,the more functions.
  
Components:
  1>.stepper motor  x4
  2>.wheel  x4
  3>.motor fixed block  x4
  4>.100x213x5mm perspex sheet  x1
  5>.100x213x5mm perspex sheet  x1
  6>.L298N driver board  x1
  7>.Arduino uno 328 controller  x1
  8>.Arduino sensor shield V5  x1
  9>.PTZ  x1
  10>.Servo motor  x1
  11>.Ultrasonic module  x1
  12>.Three channel tracking module  x1
  13>.Infrared receive sensor  x1
  14>.Transducer  x1
  15>.18650 battery holder  x1
  16>.18650 battery  x2
  17>.18650 battery charger  x1
  18>.Bluetooth module  x1
  19>.Dupont wire  x30
  20>.USB cable 1M  x1
       21>.Copper cylinder M3*20mm x3
  22>.Copper cylinder M3*35mm  x6
  23>.Copper cylinder M3*20mm  x3
  24>.Copper cylinder M3*6mm  x6
  25>.M3 3mm screw and nut
  
Outline:
1.PTZ and servo motor Installation
2.Ultrasonic Installation
3.Car Chassis
4.Introduction of L298N motor driver module, Tracking module principle,
Ultrasonic obstacle avoidance intelligent car, Mobile phone bluetooth intelligent
car

  

Step 1: PTZ and Servo Motor Installation Instruction


Step 2: Install As Below:

Step 3: Install the Frame

Step 4: Fix Ultrasonic

Step 5: Add It to the Car Chassis

Step 6: Experiment in Details

1>.L298N motor driver module

ENA(yellow in picture):
1(5V/PWM):enable motor A
0(GND/PWM):disenable motor A
IN1 to 5V,IN2 to GND,motor A corotation IN1 to GND,IN2 to 5V,motor A
rollback ENB(yellow in picture):
1(5V/PWM):enable motor B
0(GND/PWM):disenable motor B
IN3 to 5V,IN4 to GND,motor A corotation
IN3 to GND,IN4 to 5V,motor A rollback

5V_EN(green in picture):
If use the jumper, chip 78 m05 provide power supply for modules
If do not use jumper, need to use 5v-pin & GND-pin provides power supply
module for modules

CSA/CSB(green in picture):
Current test pins for motor A/B,can series connection resistance
If do not use jumper, detection of the current
If use the jumper,not detect current

UR1-UR4(green in picture):
Choose whether to use pull-up resistor
For I/O port driver ability of microcontroller, can short circuit, using pull-up
resistor
If use the jumper, Do not use the pull-up resistor. If do not use jumper ,use the
pull-up resistor
NOTE:You can use other code to driver motor.

2>.Tracking smart car


  
Tracking module principle:TCRT5000 Using infrared reflectivity of color is
different, the strength of the reflected signal is converted into electrical signals.
Black and white tracing module in high level effectively detect black, white is
detected for the low level, effectively detect 0-3 cm in height.
  
  
3>.Ultrasonic obstacle avoidance intelligent car

Obstacle avoidance intelligent ultrasonic is convenient, simple and easy to do


real-time control, and can meet the practical requirements in terms of accuracy of
measurement, thus become a commonly used method of obstacle avoidance.
Ultrasonic method using reference (Arduino ultrasonic ranging).
Ultrasonic smart wiring diagram;
  
4>.Mobile phone bluetooth intelligent car

Bluetooth (Bluetooth) technology, is actually a short-range radio technology,


using the" Bluetooth "technology, can effectively simplify the handheld
computers, laptops and mobile phones for communication between mobile
phones and other mobile communication terminal equipment, also can
successfully simplify the above communication between the devices and the
Internet (Internet), so that the modern communication equipment and data
transmission between the Internet become more efficiently, to broaden the way
with wireless communications.

Because today is the first time to deal with the bluetooth module, or to a


profound, let the Arduino and PC communication success. Wiring, first connect
bluetooth motherboard + 5 v VCC, bluetooth motherboard GND connection -
GND, motherboard TX connected bluetooth RX, RX connected bluetooth TX.
When the success of the bluetooth module when the power is connected to the
PC, the bluetooth module power will flashing lights, connect light green light
will light up.

Let's learn the Arduino bluetooth remote control programmable intelligent car.
Through bluetooth control forward, backward, turn left, turn right, buttons,
computers and mobile phones, two kinds of control mode. (mobile phone
operating system support Android 2.3.7 above. The computer must bring their
own bluetooth)

When used for the first time to mobile phone with bluetooth car matching
(pairing for the first time after the later don't have in wireless location), first take
a look at the following steps:
  1>>.Remember to open mobile phone bluetooth oh, open the software will
remind users open the bluetooth
  2>>.Then the text as shown in figure tips, connect a bluetooth device, scan
matching bluetooth oh, otherwise not be able to connect to the car.
  3>>.Match the car, the password is "1234" try it

The code for reference:


http://www.icstation.com/ebay/IC/All%20data%20modules/2529.zip
  
Thank you for your visiting! Please feel free to share us your comments. :)

Should you meet any problem, please feel free to contact us.

ICStation Team
  
  
  
Comments
ino Robots You Can Build for Less than $125READ MORE

Here’s what we have, after taking everything out of the packaging:

To get started, we’ll attach the motors and the H bridge (the card that delivers power to
the motors) to the lower part of the chassis. First, attach the four metal brackets (they’re
rectangular, drilled blocks of metal) to each motor using two long bolts and two nuts.
You’ll need to make sure that they’re attached correctly, so check out the image below
to make sure that the side of the block with two drilled holes will face downward. Note
that the wires on each motor are pointing toward the center of the chassis.
Now each motor can be attached to the chassis by using two short bolts in the bottom of
each metal bracket. Here’s a view of the bottom of the chassis so you can see where the
bolts need to be:
The next step is to secure the H bridge (that’s the red board, in my kit) to the
chassis. You may want to wait until all of the wires are attached to the H bridge before
doing this, but that’s up to you (I found it to be easier). A quick note: my kit was
missing a number of fasteners, so I used electrical tape to secure the bridge. However,
you can see here where the bolts and nuts would have gone:
Now that the H bridge has been attached, you can start wiring up the power
supply. Because the six-AA battery holder comes with a DC adapter, you’ll need to
either cut off the end (which I did) or run jumper wires to the batteries themselves.
No matter how you decide to do it, you’ll run the positive wire to the port labelled
“VMS” and the negative wire to the one labelled “GND” on the bridge. Screw down the
fasteners and make sure they’re secure. Then, you’ll connect the motor wires. On both
sides, there’s a set of two ports; one is labelled “MOTORA” and the other
“MOTORB.” Both red wires on each side will go into the centermost green port, and
both black wires will go into the outermost. This picture should make it more clear:
I found that I had to strip some of the housing off of the motor wires to get this to
work. Now that you have the motors and the power supply all wired up, slide the wheels
onto the motor drive shafts, and attach the four copper shafts in the locations show in
the picture below (each copper shaft needs one small bolt). This robot is starting to take
shape!
Now, set that part of the chassis aside and grab the other one which will sit on top. The
next step is to attach the Arduino—again, I had to use electrical tape, but you should be
able to better secure yours with some bolts and nuts.
The next step requires the micro servo, the black crosspiece, the servo holder (which
consists of three black plastic pieces), and some small screws. Use one of the larger
sharp screws in the kit to attach the black crosspiece to the micro servo:
Then flip the servo upside down into the black plastic ring of the holder. Make sure that
the wires coming out of the servo are facing in the same direction as the longer part of
the holder (again, see the image below), and use four tiny screws to secure the crossbar
(there are four holes in the holder that align with the holes on the crossbar).
Here’s what it looks like after it’s attached:
Finally, take the other two pieces of the servo holder and snap them onto the servo
(there are grooves in the side pieces that match the plastic tab on the servo).
Now that the servo holder is complete, it can be mounted to the chassis.
Here’s where the bolts go:
It’s time to give our robot some eyes. Attach the ultrasonic sensor to the servo holder
using two zip ties.
If you’re working from the same kit as I am, you’ll have received an Arduino sensor
shield. We won’t be using it in this build, but you can pop it on top of the UNO now if
you want (as I have in the image below). Just align the pins on the bottom of the shield
with the I/O ports on the Arduino and press down to connect them. You don’t need it at
the moment, but shields can come in handy.The Top 4 Arduino Shields To
Superpower Your Projects The Top 4 Arduino Shields To Superpower Your
 

ProjectsYou’ve bought an Arduino starter kit, you’ve followed all the basic guides, but now
you’ve hit a stumbling block - you need more bits and bobs to realise your electronics
dream. Luckily, if you have...READ MORE
Whether you connect a sensor shield or not, you’ll now need four wires to connect the
ultrasonic sensor to the Arduino. There are four pins on the sensor, VCC, GND, TRIG,
and ECHO. Connect VCC to the 5V pin on the Arduino, GND to GND, and TRIG and
ECHO to I/O pins 12 and 13.

Now grab the lower part of the chassis, and connect six jumper wires to the I/O pins of
the H bridge (they’re marked ENA, IN1, IN2, IN3, IN4, and ENB). Take note of which
color wires are connected to which ports, as you’ll need to know later.
Now it’s time to start putting this thing together. Grab the upper part of the chassis and
set it on top of the copper shafts connected to the lower part, and pull the wires attached
to the H bridge through the hole in the center of the chassis. Connect the six wires to I/O
ports as follows:

 ENA to I/O port 11


 ENB to I/O port 10
 A1 to I/O port 5
 A2 to I/O port 6
 B1 to I/O port 4
 B2 to I/O port 3
Now, use four short screws to attach the upper part of the chassis to the copper
shafts. Set the six-AA battery holder on top of the chassis (screw it down if you can),
attach the 9V cell holder to the Arduino, and this bot is ready to rock!
Well, almost ready to rock. It doesn’t have quite enough personality yet.
There we go. Now to give it a brain. Let’s do some programming.

The first thing we’ll do is test to make sure that the bridge and motors are hooked up
correctly. Here’s a quick sketch that will tell the bot to drive forward for half a second,
drive backward for half a second, then turn left and right:

// Motor A pins (enableA = enable motor, pinA1 = forward, pinA2 =


backward)
int enableA = 11;
int pinA1 = 6;
int pinA2 = 5;
//Motor B pins (enabledB = enable motor, pinB2 = forward, pinB2 =
backward)
int enableB = 10;
int pinB1 = 4;
int pinB2 = 3;
//This lets you run the loop a single time for testing
boolean run = true;
void setup() {
pinMode (enableA, OUTPUT);
pinMode (pinA1, OUTPUT);
pinMode (pinA2, OUTPUT);
pinMode (enableB, OUTPUT);
pinMode (pinB1, OUTPUT);
pinMode (pinB2, OUTPUT);
}
void loop() {
if (run) {
delay (2000);
enableMotors();
//Go forward
forward (200);
//Go backward
backward (200);
//Turn left
turnLeft (400);
coast (200);
//Turn right
turnRight (400);
coast (200);
//This stops the loop
run = false;
}
}
//Define high-level H-bridge commands
void enableMotors()
{
motorAOn();
motorBOn();
}
void disableMotors()
{
motorAOff();
motorBOff();
}
void forward (int time)
{
motorAForward();
motorBForward();
delay (time);
}
void backward (int time)
{
motorABackward();
motorBBackward();
delay (time);
}
void turnLeft (int time)
{
motorABackward();
motorBForward();
delay (time);
}
void turnRight (int time)
{
motorAForward();
motorBBackward();
delay (time);
}
void coast (int time)
{
motorACoast();
motorBCoast();
delay (time);
}
void brake (int time)
{
motorABrake();
motorBBrake();
delay (time);
}
//Define low-level H-bridge commands
//enable motors
void motorAOn()
{
digitalWrite (enableA, HIGH);
}
void motorBOn()
{
digitalWrite (enableB, HIGH);
}
//disable motors
void motorAOff()
{
digitalWrite (enableB, LOW);
}
void motorBOff()
{
digitalWrite (enableA, LOW);
}
//motor A controls
void motorAForward()
{
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, LOW);
}
void motorABackward()
{
digitalWrite (pinA1, LOW);
digitalWrite (pinA2, HIGH);
}
//motor B controls
void motorBForward()
{
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, LOW);
}
void motorBBackward()
{
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, HIGH);
}
//coasting and braking
void motorACoast()
{
digitalWrite (pinA1, LOW);
digitalWrite (pinA2, LOW);
}
void motorABrake()
{
digitalWrite (pinA1, HIGH);
digitalWrite (pinA2, HIGH);
}
void motorBCoast()
{
digitalWrite (pinB1, LOW);
digitalWrite (pinB2, LOW);
}
void motorBBrake()
{
digitalWrite (pinB1, HIGH);
digitalWrite (pinB2, HIGH);
}
view raw4wd-robot-test.ino hosted with ❤ by GitHub

That’s a lot of code for a simple test, but defining all of those functions makes it
easier to tweak later. (Big thanks to Billwaa for his blog post on using the H-
bridge for defining these functions.) If something went wrong, check all of your
connections and that the wires are connected to the correct pins. If everything
worked, it’s time to move onto the sensor test. To use the ultrasonic sensor,
you’ll want to download the NewPing library, and then use Sketch > Include
Library > Add . ZIP Library… to load the library.

Make sure that you see the include statement at the top of your sketch; if you don’t,
hit Sketch > Include Library > NewPing. Once you’ve done that, load up the
following sketch:

#include <NewPing.h>
//Tell the Arduino where the sensor is hooked up
NewPing sonar(12, 13);
long inches;
void setup() {
//Activate the serial monitor so you can see the output of the
sensor
Serial. begin (9600);
}
void loop() {
delay (50);
//Ping the sensor to determine distance in inches
inches = sonar. ping_in();
//Print the distance in inches to the serial monitor
Serial. print (inches);
Serial. print (" in.");
Serial. print ("\n");
}
view rawnewping_demo.ino hosted with ❤ by GitHub

Upload the sketch, and open up the serial monitor using Tools > Serial Monitor. You
should see a rapidly updating sequence of numbers. Hold your hand in front of the
sensor and see if that number changes. Move your hand in and out, and you should get a
measurement of how far away your hand is from the sensor.

If everything worked correctly, it’s time to put it all together and let this thing
run! Here’s the code for the robot now. As you can probably tell, this is basically the
two test sketches put together with an added if statement to control the robot’s
behavior. We’ve given it a very simple obstacle-avoidance behavior: if it detects
something less than four inches away, it will backup, turn left, and start moving
again. Here’s a video of the bot in action.

Give Your Robot Some Life


Once you’ve gotten this behavior working correctly, you can add more complex
behavior; make the robot alternate between turning left and right, or choose
randomly; sound a buzzer if it gets close to something; just turn, instead of backing
up; you’re really only limited by your imagination. You could use just about anything in
your Arduino starter kit to add more functionality. You’ll notice also that we haven’t
coded anything for the servo yet: you can actually makes your robot’s “eyes” move
back and forth. perhaps using them to seek out a path instead of just backing up
whenever it finds an obstacle directly in front.What's in Your Arduino Starter
Kit? [Arduino Beginners] What's in Your Arduino Starter Kit? [Arduino
 

Beginners]Faced with a box full of electronic components, it's easy to be overwhelmed. Here's a


guide to exactly what you'll find in your kit.READ MORE

Let us know if you decide to build this robot or another one, and tell us how you decide
to customize its behavior or looks. If you have any questions about this robot, post
them in the comments below, and I’ll see if I can help!

You might also like