Ultrasonic sensor and vibrating motor are not working

Hello. I'm doing a school project and I'm new to Arduino boards and those related things. My goal was to create a device using my Arduino Mega 2560 board, with an ultrasonic sensor and a coin vibrating motor attached. It would alert the user of objects in their surroundings, by sending different vibrations through the motor. However, none of it seems to be working. The sensor isn't sending signals, and the motor isn't vibrating. I'd also like to mention that I am using the NewPing library. Here's what I have:

  • Arduino Mega 2560 board
  • HC-SR04 ultrasonic sensor
    - VCC plugged into 5v
    - Trig plugged into 27
    - Echo plugged into 28
    - GND plugged into GND
  • 3v coin vibrating motor (two wires)
    - red (+) plugged into A8
    - blue (-) plugged into GND

This is the code:

1 #include <NewPing.h>
2
3
4 #define trigPin 27
5 #define echoPin 28
6 #define MAX_DISTANCE 500
7 int motorPin = A8;
8
9 NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
10
11
12 float duration, distance;
13
14 void setup() {
15 Serial.begin(9600);
16 }
17
18 void loop() {
19 delay(50);
20
21 duration = sonar.ping_cm();
22 distance = ((duration / 2) * 0.0343);
23 Serial.print("Distance = ");
24 Serial.print(distance);
25 Serial.println(" cm");
26
27 if (distance > 500){
28 digitalWrite(motorPin, HIGH);
29 delay(500);
30 digitalWrite(motorPin, LOW);
31 delay(500);
32 } else if (distance > 300 && distance <500) {
33 digitalWrite(motorPin, HIGH);
34 delay(500);
35 digitalWrite(motorPin, LOW);
36 delay(500);
37 digitalWrite (motorPin, HIGH);
38 delay(500);
39 digitalWrite(motorPin, LOW);
40 } else if (distance > 150&& distance <300) {
41 digitalWrite(motorPin, HIGH);
42 delay(500);
43 digitalWrite(motorPin, LOW);
44 delay(500);
45 digitalWrite (motorPin, HIGH);
46 delay(500);
47 digitalWrite(motorPin, LOW);
48 delay(500);
49 digitalWrite(motorPin, HIGH);
50 delay(500);
51 digitalWrite(motorPin, LOW);
52 delay(500);
53 } else {
54 digitalWrite (motorPin, HIGH);
55 delay(1000);
56 digitalWrite(motorPin, LOW);
57 delay(500);
58 }
59
60 }
61

How do you know this?
Do you have exceptional hearing?

Please remember to use code tags when posting code.

Should I jump for joy?
Everybody else is.

It doesn't show any errors in my code, that's why I'm confused as to it not working. There's no need for harsh remarks. It was my understanding that the TX light would be flashing if it were sending signals.

So, when you ran the simple example from the NewPing for a single sensor, that ran correctly and displayed results in the serial monitor?

(Doesn't it get tiresome when pigeons keep running to you?)

No, hence my confusion. There were no results of it sending or receiving anything. But, there were no errors in the code. Can I block you, you aren't of help in any way and are just be obnoxious. Really don't you have anything better to do?

So, when the basic example code written by the author of the library didn't work, you thought you'd just press on, rather than finding out why?

Can we expect those code tags anytime soon?

(It's true - I've not seen nothing like the Mighty Quinn)

Hi!

Welcome to the Forum.

In order to save your time and the other´s that will try to help you, it´s higly recommended that you read this topic before next posting:

How to get the best out of this Forum

I´ll consider that your wiring is ok, since you provided no schematics or photos.

There are some issues in you code that you should take a look:

21 duration = sonar.ping_cm();     //this line already gives you a distance in cm
22 distance = ((duration / 2) * 0.0343);    // which makes this line nonsense
27 if (distance > 500){  //this will never happen, since you defined MAX_DISTANCE 500

PS: Ultrassonic sensor returns zero if object is out of range and it will not reach 500cm.

Useful tip is to divide your projects in small steps: learn to use HC-SR04 only; learn to use coin vibrating motor only; then put both together. This is why @anon73444976 is suggesting you to begin with the examples of NewPing library.

You understand that because your code compiles and links with no errors indicated by the IDE does not mean it will work as you intended right? You could also have a wiring issue, etc.

Use a sample program to test each device independently to ensure the hardware is working correctly. Then, and only then, attempt to debug your entire program. This is the way I debug every time I start a new project and I have been developing embedded applications for over 30 years.

Yes. What else do we have to be happy about?

That's missing stuff.
Waggling the input pullup isn't going to drive a motor.
OTOH, the pullup is limiting the current, which is a Good Thing(TM)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.