Controlling speed of DC motor with Ultrasonic sensor

Hello all, I'm trying to figure out how to use my current Tinkercad configuration to have the distance read from an ultrasonic sensor and cause a DC motor to spin faster based on this distance. I've experimented with it and used the digitalWrite command but I understand that this does not change the RPM of the motor of PWM, so I am sort of lost because analogWrite has not worked either (I've tried connecting to the analog pins too). I am only two weeks into using anything related to Arduino and Tinkercad so I apologize if my ignorance is showing here. I would appreciate any help! Below is my circuit and my code.

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

long duration; 
int distance; 
int pinNum = 13;
const int pinPi = 12;
const int motor = 7;

void setup() {
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);
  pinMode(pinNum, OUTPUT);
  pinMode(pinPi, OUTPUT);
  pinMode(motor, OUTPUT);
  
  Serial.begin(9600); 
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); 
  Serial.println("with Arduino UNO R3");
}
void loop() {
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2; 
  
  if(distance <= 80) {
    
  	digitalWrite(pinNum, HIGH);
  	delay(300);
  	digitalWrite(pinNum, LOW);
  	delay(300);
  	digitalWrite(pinNum, HIGH);
    delay(300);
  	digitalWrite(pinNum, LOW);
  	tone(12 ,200 ,200);
  	delay(100); 
	}
  else if (distance <= 140) {
  	digitalWrite(pinNum, HIGH);
  	delay(750);
  	digitalWrite(pinNum, LOW);
  	delay(750);
  	digitalWrite(pinNum, HIGH);
  	delay(750);
  	digitalWrite(pinNum, LOW);
  	tone(12 ,200 ,100);
  	delay(300); 
    }
  else {
  	digitalWrite(pinNum, HIGH);
  	delay(1200);
  	digitalWrite(pinNum, LOW);
  	tone(12 ,200 ,100);
  	delay(500); 
    }
 

}

But, what was the problem exactly?

Hi!

by doing this:

the result of distance will be an integer (int) but you want a decimal number as a result.
Instantiate distance value with:

float distance;

I propose you export the distance value to the serial monitor in order to check the actual calculated value.
Furthermore, the digitalWrite() function will allow a value of 0 or 1, so your pin will be on or off only.
To set up specific speed, you can use
analogWrite(pinNum, PWM_VALUE);

where PWM_VALUE ranges from 0 (no current) to 255 (max current)

EDIT: it seems you wire your motor with pin 7 wich is digital only (so able to output 0 or 1 only). Switch to pin 6 and modifiy the code in accordance.

const int motor = 6;

You cannot directly drive a motor from an Arduino pin. You will be very lucky not to kill the pin. In order to control a motor speed in one direction of notation you need a drive that contains, at least, a switch and a flyback diode. The switch controls current from an external power supply and the diode protects the switch from the high reverse voltage that occurs when power is removed from the motor windings. The switch can be a relay or transistor. Relays are ill suited for motors that will have PWM speed control.


MOSFET motor driver


NPN driver for small hobby motor

Thank you! I had no clue that this pin was digital only. This makes sense as to why it would not work with the PWM values I was trying to send.

cool. but take time to read the @groundFungus is you need to power up a motor this way.

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