0% found this document useful (0 votes)
8 views5 pages

C Experiment 5

Uploaded by

Rahul Rajagopal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

C Experiment 5

Uploaded by

Rahul Rajagopal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment - 5

Aim - Integrate the concept of experiments number 3 with 4 and control the motor speed using
Ultrasonic sensor.

Objectives –

1. To control the rpm of a motor based on signal input using Ultrasonic sensor/ IR sensor.

Components Required –

1. DC Motor/ Servo Motor


2. Ultrasonic Sensor/IR Sensor
3. Jumper Wire
4. Breadboard
5. USB Cable
6. Arduino
7. 12V Battery (If motor is more than 5V)

Circuit Diagram and Theory –

Circuit Diagram:

Figure .1: Circuit Diagram of sensor and motor integration using Arduino.
Steps:

Note: This step is for multiple motor. You have to reduce for single motor.

 Connect the first motor to motor controller module Out1 and Out2.
 Connect the first motor to motor controller module Out3 and Out4.
 Connect the positive wire from the battery pack to pin +12V on the module.
 Connect the negative wire from the battery pack to pin GND on the module.
 Connect the GND pin of the module to the GND pin of the Arduino.
 Connect Arduino pin 5 to module pin In1.
 Connect Arduino pin 4 to module pin In2.
 Connect Arduino pin 3 to module pin In3.
 Connect Arduino pin 2 to module pin In4.
 Connect the sensor pin Vcc to Arduino pin 5V.
 Connect the sensor pin GND to Arduino pin GND.
 Connect the sensor pin Trig to Arduino pin 13.
 Connect the sensor pin Echo to Arduino pin 12.
 Inspect the wiring and ensure they are correct.

Code:

First Method:

1. #define trigPin 13
2. #define echoPin 12
3. #define motorPin 8
4. void setup()
5. {
6. Serial.begin (9600);
7. pinMode(trigPin, OUTPUT);
8. pinMode(echoPin, INPUT);
9. pinMode(motorPin, INPUT);
10. }
11. void loop ()
12. {
13. int duration, distance;
14. digitalWrite (trigPin, HIGH);
15. delayMicroseconds (1000);
16. digitalWrite (trigPin, LOW);
17. duration = pulseIn (echoPin, HIGH);
18. distance = (duration/2) / 29.1;
19. if (distance < 20) { // Distance from sensor
20. digitalWrite (motorPin, HIGH); // When in range, motor should start high
21. }
22. else
23. {
24. digitalWrite(motorPin, LOW);
25. }
26. if (distance > 20) { // Distance from sensor
27. Serial.println("Out of range");
28. }
29. else {
30. Serial.print(distance);
31. Serial.println(" cm");
32. }
33. delay(500);
34. }

Second Method:

1. #define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04


2. #define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
3. long duration;
4. int distance;
5. int pinNum = 13;
6. const int pinPi = 12;
7. const int motor = 7;
8. void setup()
9. {
10. pinMode(trigPin, OUTPUT);
11. pinMode(echoPin, INPUT);
12. pinMode(pinNum, OUTPUT);
13. pinMode(pinPi, OUTPUT);
14. pinMode(motor, OUTPUT);
15. Serial.begin(9600);
16. Serial.println("Ultrasonic Sensor HC-SR04 Test");
17. Serial.println("with Arduino UNO R3");
18. }
19. void loop()
20. {
21. digitalWrite(trigPin, LOW);
22. delayMicroseconds(2);
23. digitalWrite(trigPin, HIGH);
24. delayMicroseconds(10);
25. digitalWrite(trigPin, LOW);
26. duration = pulseIn(echoPin, HIGH);
27. distance = duration * 0.034 / 2;
28. if(distance <= 80)
29. {
30. digitalWrite(pinNum, HIGH);
31. delay(300);
32. digitalWrite(pinNum, LOW);
33. delay(300);
34. digitalWrite(pinNum, HIGH);
35. delay(300);
36. digitalWrite(pinNum, LOW);
37. tone(12 ,200 ,200);
38. delay(100);
39. }
40. else if (distance <= 140) {
41. digitalWrite(pinNum, HIGH);
42. delay(750);
43. digitalWrite(pinNum, LOW);
44. delay(750);
45. digitalWrite(pinNum, HIGH);
46. delay(750);
47. digitalWrite(pinNum, LOW);
48. tone(12 ,200 ,100);
49. delay(300);
50. }
51. else {
52. digitalWrite(pinNum, HIGH);
53. delay(1200);
54. digitalWrite(pinNum, LOW);
55. tone(12 ,200 ,100);
56. delay(500);
57. }
58. }
Additional Tasks –
1. Speed control of motor using push button that represents Emergency Stop.
Conclusions -

You might also like