I am trying to create sort of a belt for the blind for a school project using ultrasonic sensors and pager motors. If the ultrasonic sensor detects an object the pager motor powers on. However, I am having issues with getting the pager motor and ultrasonic sensor to cooperate.
I namely have two problems
- When using an external power source of 14v (which I will need for multiple pager motors) the ultrasonic sensor outputs an incorrect distance value of 1 centimeter. What can I do to fix this?
- How do I set up the board so the ultrasonic sensor and pager motor cooperate? As of now, I have the pager motor in a circuit that a LED would be in ( a resistor to the ground and vcc to digital write). Is this incorrect? What would be appropriate?
Below is the code and an image of my board.
Any help would be appreciated, Thanks.
const int trigPin = 9;
const int echoPin = 10;
const int motorPin = 11; // Change this to the pin where your pager motor is connected
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorPin, OUTPUT); // Set motor pin as output
}
void loop() {
long duration, inches, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
// Check if object is within 15cm
if (cm <= 15) {
// Turn on the pager motor
digitalWrite(motorPin, HIGH);
delay(50); // Keep the motor on for 50 milliseconds
digitalWrite(motorPin, LOW);
} else {
digitalWrite(motorPin, LOW); // Turn off the motor
}
delay(100);
}
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}