Project 9 extra challenge

I get the concept of the extra challenge question on project 9. Activating the transistor using a PWM pin, and I have done that using the:

analogWrite(motorPin, 255); or 90 or 0 etc...

but how would I run that using a potentiometer through the Analog input? I would need to calculate the Analog input 0 - 1023(?) and then correlate that with a PWM 0 - 255,
I'm just a bit uncertain as to what to type in to program that.

Does this make any sense?

Thanks

The challenge is:
Try hooking up a potentiometer to an analog input and use that to PWM the pin that controls the transistor.

my current sketch code is:

const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;
void setup() {
// put your setup code here, to run once:
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(switchPin);

if(switchState == HIGH) {
analogWrite(motorPin, 90);
}
else{digitalWrite(motorPin, LOW);}
}

Have you done an analog read exercise?

The potentiometer can return a value of 0 - 1023. The speed of the motor on PWM takes a value of 0 - 255, ie a quarter of the value of the analog read value.

In future please post any code between code tags. If you don't know how to do this then read the topic entitled 'How to use this forum'.

Thanks for the reply, I just finished the analog exercise and I have tweaked my code a bit, I'm getting mixed results.

I did read the 'How to use this forum' post, unfortunately it was after I had posted the code. I will be sure to use the proper protocol going forward.

I now am dividing the A0 by 4 and having the analogRead act on that number. What is happening is that I'm only seeing vary small variances in the Serial window, on the upper end of the pot it reads 255, on the other end it reads 251. I may be doing something wrong.

const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
int sensorValue = analogRead(A0/4);
Serial.println(sensorValue);
delay(1);
switchState = digitalRead(switchPin);

if(switchState == HIGH) {
  analogWrite(motorPin, sensorValue);
}
else{digitalWrite(motorPin, LOW);}
}

Close! You are reading the value of (pin A0 divided by four), rather than (pin A0) divided by four!

One way to do it, propably a better way available from someone else.

I added an extra transistor for the potentiometer and a capacitor 10µF that takes away the remaining current that's left in the circuit so the engine stops faster and smoother.

KLHL:
One way to do it, propably a better way available from someone else.

I added an extra transistor for the potentiometer and a capacitor 10µF that takes away the remaining current that's left in the circuit so the engine stops faster and smoother.

Tinkercad link

This works perfect.

const int motorPin = 9;
const int potPin = A0;
int motorSpeed = 0;
int potVal = 0;


void setup() {
  
  pinMode(motorPin, OUTPUT);

}

void loop() {
   
  potVal = analogRead(potPin);
  
  motorSpeed = map(potVal, 0, 1023, 0, 255);
  
  analogWrite (motorPin, motorSpeed);

I'm commenting to unlock the thread at the request of a forum user.

pert:
I'm commenting to unlock the thread at the request of a forum user.

Thank you for unlocking :slight_smile:

I want to share the code from the Project 9, where a potentiometer is added to control the speed of the motor.
Unfortunately, all posts in this thread ignores the button.

With this version you can control the motor speed but the motor only starts when the button is pushed.

The Poti is added on Analog Input 0 & the If-Statement includes the desired motor speed converted with the map() function.

Here is the complete code with comments :slight_smile:

/* 
Project 09
Motorized wind turbine
14.01.2020

Transistors, high currents.
+ Poti is added to control the motor speed.
*/

// Constants and variables

const int switchPin = 2;
const int motorPin = 9;
int switchState = 0;

// Add Poti & define variables

const int potPin = A0;
int potVal = 0;
int motorSpeed = 0;

// Setup //

void setup() {

  // I/O of the pins

  pinMode(switchPin, INPUT);
  pinMode(motorPin, OUTPUT);

  // Set serial connection to track values

  Serial.begin (9600);

}

// Loop //

void loop() {

   // Read & assign poti values

  potVal = analogRead(potPin);
  Serial.print("potVal: ");
  Serial.println(potVal);
   
   // Use map() function to convert the analog signal into PWM

  motorSpeed = map(potVal, 0, 1023, 0, 255);

  Serial.print("motorSpeed: ");
  Serial.println(motorSpeed);
   
   // Read the state of the button & set to HIGH when pushed

   switchState = digitalRead(switchPin);

   Serial.print("switchState: ");
   Serial.println(switchState);

  if (switchState == HIGH) {              // Button
    digitalWrite(motorPin, HIGH);
    analogWrite(motorPin, motorSpeed);  // Poti (added to the if-statement to control the motor speed) 
  }

  else {
    digitalWrite(motorPin, LOW);
  }
  
}

To hook up the poti, I used the shematic of project 5.

To ease things up, I made a small design:

Dropbox Link of Image

I hope this helped.
Keep coding!

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