My son and I are working on a project and neither of us are great at coding. We're wanting to control the DC motor's speed at various levels in accordance to the ultrasonic sensor. For example, at 10-6 feet, motor barely turns, 6-3 feet, motor level at half speed, and 3-0 feet, motor at full 255. Any help would be very appreciated!
Suggest you use a 360 degree servo (continuous rotation servo).
The distance sensor would be used to control the servo speed and forward to reverse action, using servo.write(position) with 90° as stopped.
Maybe something like this...
loop()
{
// Get the distance from the sensor.
distance = getDistance();
// Set the required speed based on distance.
if (distance <= 3.0)
speed = 255;
else if (distance <= 6.0)
speed = 128;
else if (distance <= 10.0)
speed = 64;
else
speed = 0;
// Set motor speed.
setMotorSpeed(speed);
}
As you're using a DC motor, you can easily do your task with a motor driver module(L293D/L298). If you're using Arduino UNO, you have to connect the Enable pin of the motor driver to any PWM pin of the Arduino. Thus you will achieve speed control of the motor by PWM.
Firtsly, OP said nothing about the need for direction control. A mosfet would be enough if no reversal is required, but @imreebs perhaps you should just confirm that.
And secondly, there are many better (more efficient) drivers than the steam-driven 293/298.
This diagram (source) shows how to connect a motor via a mosfet, just in case you need this info.
Then just analogWrite(speed) to the pin where the mosfet gate is connected.
Your Arduino should never be used to power motors or high current loads.
I'm using the breadboard power supply but I didn't see it in tinkercad
These are usually (unless heat sinked) meant for low current loads too.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.