How can I implement a distance-based vibration motor intensity using an ultrasonic sensor?

Hi, I am new to Arduino but I have a project that I want to achieve. Basically, I have an Ultrasonic sensor (HC-SR04) and I would like to control the intensity of my mini vibration motor based on the distance provided by the sensor.

Which means the closer you are to the sensor, the higher the intensity of the vibration motors get. But I'm kind of having a hard time applying it as I am new to Arduino. I saw something about PWM but I kind of worried to try it as I might fry my components as I have zero knowledge with this.

Any help would be appreciated, Thanks!

Hi!

I am sure your project is feasable, and easy, because as you say, it involves the ultrasonic sensor and its values, your motor, and a link between the 2. PWM could be a perfect fit.

About your code concept, here is how I would work:

  • plug the HC-SR04 to the card, make some experiment and export values (in milliseconds and in centimeters for example) to serial monitor;
  • in another hand, plug your motor and drive it at different speed (there are PWM) while exporting the driven value to the serial monitor;

you should obtain 2 ranges of values: distance measured by ultrasonic, and PWM sent to the motor.
The final goal is to link the 2 variables.

Did you try a code yet? Can you provide us:

  • the code you tried with the correct formatting for the forum (use the </>)
  • a schema (even hand drawn) of your circuit wich shows the components, the board, and the way you power everything
  • model of your board.

EDIT: to make some test with PWM with arduino, it is quite easy

pinMode(Pin_Motor, OUTPUT); // the pin wich connect the motor and the board
analogWrite(Pin_Motor, VALUE_IN_PWM_TO_APPLY); // value ranges from 0 (STOP) to 255 (max current, equivalent to: `digitalWrite(Pin_Motor, HIGH);`

Hi, Thanks for responding! I really appreciate it.

I have not made any schemas or code yet as I have no idea how to implement it.

But as you have instructed, I have tried PWM and finally understood how it works!

My next hurdle is linking the two. How can I link the vibration motor and the ultrasonic sensor? Are there any resources or code that I can do a trial and error with? I do not know which function to use to achieve it.

Good. always try to make experiment by your own.

Actually, please consider the different part of your code, according to your needs:

  • to get the distance from the ultrasonic sensor;
  • to convert the distance into a PWM value;
  • to apply the PWM value to the motor.

in a high level, your code should look like this:

Loop(){
GET_DISTANCE();
CONVERT_DISTANCE_IN_PWM();
DRIVE_MOTOR_WITH_PWM();
}

of course, you can imagine the sweet things are included into this 3 generic functions but I wanted to show you a clean way to distinguish action with separated functions.

Now the core:

  • for the ultrasonic sensor itself, I want you to make the job. There is plenty of example in the web, I porpose you to test the sketch. Take time to look in the code what are the inputs and outputs, it is very important as you need a lenght (in cm or meters, your choice) BUT the sensor won't give you a direct result.
    a hint: an ultrasonic sensor will probably use a function call pulseIn(). seek for it!
  • for the conversion, I propose you use the function map(). Take time to see how to use it (map() - Arduino Reference). Basically, you define the entry range of you value to be converted, a range for your result, and the map function retunrs you a result.
    As you explained that the closest to the sensor, the Higher the motor vibrates.... well let's see how you get out! :smile:
  • for the PWM, you said you test it and it works. Good job.

EDIT: my malicious pleasure to NOT give you a working code.

Hi, again, I'm really thankful for the quick responses.

Actually no! I'm not asking for a direct code for my project as I'd love to understand it myself! Thank you so much for providing an idea how it will run down!

I'll let you know if I made it work! :slight_smile:

I hope so!
To bring effective help to motivated beginners is a great reward!