DC motor speed control with volumepedal

Dear all,

I’m trying to control the speed of my DC motor with a volumepedal (behringer FCV100). For this I’m using an Arduino Uno and an Arduino Motor Shield.
With my current setup and code the motor start turning sometimes at different speeds, but only when I change the tilt of the volume pedal. When I stop moving the pedal, the motor also stops.
What I want to do is to have a continuous speed which is correlated to the tilt of my pedal.

Here’s some information on my current setup:

I cut a mono jack cable and connected the two sides on my volume pedal.

connection INPUT:
sleeve → GROUND
tip → 5V

connection OUTPUT:
sleeve → GROUND
tip → analogPin 5

(I followed this example: https://little-scale.blogspot.ch/2010/01/how-to-use-passive-volume-control-pedal.html)

My DC motor is connected to the Channel A of my motor shield.

(the basic on controlling with a Motor Shield I found here: http://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/)

In attachment is the file of my current code.

Thank you all for your time and help!

Volumepedal.ino (747 Bytes)

Please post your code inline using code tags rather than attaching it. So it looks like this:

 int analogPin = 5;

void setup() {
  
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  
}

void loop(){
  
 int volt;
  volt = analogRead(analogPin);
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A

  if (volt == 0){
     analogWrite(3, 0);  
  }

  if (volt <= 1 && volt != 0) {
     analogWrite(3, 50);   
  }

  if (volt > 1 && volt <= 2){
     analogWrite(3, 100);   
  }

   if (volt > 2 && volt <= 3){
     analogWrite(3, 150);   
  }

   if (volt > 3 && volt <= 4){
     analogWrite(3, 200);   
  }
  
  if (volt > 4 && volt <= 5){
     analogWrite(3, 250);   
  }
  


}

Several issues with that code.

  • analogPin is set to 5 which is a digital pin. Maybe you mean A5?
  • you seem to have your motor connected to pin 9 and 12.
  • yet you do a PWM output on pin 3.
  • analogRead returns a value from 0 to 1023. A value of 5 is a signal of about 25 mV. Set your intervals to multiples of 200 or so - this assuming your pedal actually returns a voltage between 0 and 5 of course.
  if (volt <= 1 && volt != 0) {

You defined volt as int. So this only evaluates as true if volt is < 0, which you never get from an analogRead().

Likewise,

  if (volt > 1 && volt <= 2){

is the exact same as:

  if (volt == 1) {

Try this

void loop() {
   adcVal = analogRead(analogPin);
   pwmVal = advVal / 4;  // converts 0-1023 to 0-255
   analogWrite(pwmPin, pwmVal); // assumes pwmPin is 9
   delay(100); // simple way to limit updates to 10 times per second
}

and move this line into setup()

digitalWrite(12, HIGH); //Establishes forward direction of Channel A

...R

Thanks a lot for your replies, they really helped my forward. My project is now working as expected.

The only problem is still that the volumepedal is giving a logarithmic signal, so the analogRead signal changes very quickly to the maximum. But I guess with the right conversion it will work.

This is my code so far (I use pin 9, 12 and 3 because these are the default pins for motor control with the arduino motor shield):

int analogPin = A0;
int pwmPin = 3; // speed control Channel A

void setup() {
  
  //Setup Channel A

  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW); //Disengage brake
}

  
void loop() {

   int pwmVal;
   int adcVal;

   adcVal = analogRead(analogPin);
   pwmVal = adcVal / 4;  // converts 0-1023 to 0-255
   analogWrite(pwmPin, pwmVal); 
   delay(100); // simple way to limit updates to 10 times per second

}