Controlling a motor using pulse sensor

Hi guys. I'm currently completing a project for university and the idea is that two motors spin with wheels on attached once the pulse sensor is pressed. The difficult part is that I haven't a clue how things like this should be connected and into what part. Can anyone therefore link me or give me some pointers as to how to connect a pulse sensor to the arduino uno board and also how to connect the two motors to the arduino motor shield which I have also.

Your help would be much appreciated :slight_smile:

What pulse sensor are you using? We need to know what type of output it has. There are also different types of pulse sensors, such as optical (which is actually an oximeter) and electrical. Are you dealing with a raw sensor that requires you to create all the circuitry to drive and amplify it, or does it have a nice clean digital output synced to one heartbeat?

http://www.active-robots.com/force-sensitive-resistor-0-5.html

Above is the link directing you pulse sensor I have ordered. All the specification for the pulse sensor is on the website.

oh... when you said pulse sensor, I was assuming you meant something that measures your pulse (heartbeat.)

So, why are you using this sensor instead of just a pushbutton? I am guessing that you want the motors to spin faster or slower depending on hard the sensor is pressed on?

Well, first things first... you need to measure the value of the sensor. To do this, you connect 5V to one side lead, gnd to the other side lead and the center lead to an ADC channel on the arduino (A0 through A7 on the Uno.) You use analogRead(A#) to get the value. It will be between 0 and 1023 depending on how hard you press on the sensor.

Then you take this value and apply it to a PWM value by using analogWrite(some pin, PWM value) You have to use a pin labeled with a ~ symbol next to it (pin 3 on the Uno for example) This will output the voltage read on the force sensor to the pin connected to the motor. Pressing harder gives a larger value on the ADC, and sending a larger value to the PWM pin means a greater voltage on the motor pin.

That's generic, I know, but that is the concept. You will have to be more specific about what you are trying to do exactly to get more than that. But, honestly "more than that" is really writing code for you. I can point you in the right direction as far as electronics to use and nudge you in the direction of how to write the code, but writing code for you will not really teach you anything.

no not that kind of pulse sensor. Turns out the 'pulse sensor' I had ordered is a force sensitive resistor and the reason as you guessed correctly is I was to be able to choose how much power the motor generates whilst I am putting more and more force on to this 'force sensitive resistor'. So far I've managed to connect the motors correctly to the motor shield and have the code that makes the motors spin however only one motor is spinning. What happens is that the one motor that is spinning spins one direction then pauses then spins the opposite direction and carries this on in a loop. I've tried switching wires over etc but the same problem is continuing. Below is the code for the motors that I have found;

void setup() {

//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin

}
void loop(){

//Motor A forward @ full speed
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 123); //Spins the motor on Channel B at half speed

delay(3000);

digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(9, HIGH); //Engage the Brake for Channel B
delay(1000);

//Motor A forward @ full speed
digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 123); //Spins the motor on Channel A at half speed

//Motor B forward @ full speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed

delay(3000);

digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(9, HIGH); //Engage the Brake for Channel B

delay(1000);

}

Which motor is spinning - does it correspond to Motor A or Motor B in the code below?

Which motor shield are you using?

Sorry for the confusion. I've checked and Motor A is spinning forwards and backwards with a delay in between the change of direction and I'm using a 6V DC motor. If you could give me some pointers on what to change in the posted code above that would be much appreciated as I'm modifying the code right now trying to see if I can hopefully make it work.

Which motor shield are you using?

If you disconnect the motor currently connected to Motor A on the motor shield and connect the other motor to those terminals, does this also produce the same behaviour?

The Arduino motor shield (A000079)

Look at the comments in the code you posted. It is describing exactly the behavior that you are experiencing, so it is meant to do that.

I know I said I wasn't going to write any code for you, but here you go. This is assuming that your motor controller actually works like this (with brake, direction, and PWM pins?) If it does not actually work like that, you may end up burning out your controller by sending the wrong signals.

If you simply turn this in for credit without really understanding it, an evil monkey will come to your home and kill a kitten.

Just to be sure, I broke it a little in a couple places on purpose and you will need to understand the code to fix it. However, my code will not break your controller (it just won't work until you fix it.)

//Define all the pins used here for easier code reading

#define MotorA 12 //pin used for Motor A direction control
#define MotorB 13 //pin used for Motor B direction control

#define BrakeA 9 //pin used for Brake A control
#define BrakeB 8 //pin used for Brake B control

#define PWMA 3 //pin used for PWM output for MotorA
#define PWMB 11 //pin used for PWM output for MotorB

#define Sensor A0 //pin used to read the pulsesensor



void setup() {

//Setup Channel A
pinMode(MOTORA, OUTPUT); //Initiates Motor Channel A pin
pinMode(BRAKEA, OUTPUT); //Initiates Brake Channel A pin

//Setup Channel B
pinMode(MOTORB, OUTPUT); //Initiates Motor Channel A pin
pinMode(BRAKEB, OUTPUT); //Initiates Brake Channel A pin

}

void loop(){

int val = getpulse();

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

digitalWrite(BRAKEB, LOW); //Disengage the Brake for Channel A
digitalWrite(BRAKEB, LOW); //Disengage the Brake for Channel B

analogWrite(PWMB, val); //Sets the speed to fully on for motor A
analogWrite(PWMB, val); //Sets the speed to fully on for motor B


}

void brake(){

analogWrite(PWMA, 0); //Stops motor A from spinning
analogWrite(PWMB,0); //Stops Motor B from spinning

digitalWrite(BRAKEA, HIGH); //Engage the Brake for Channel A
digitalWrite(BRAKEB, HIGH); //Engage the Brake for Channel B

}

int getpulse(){


int val = analogRead(4);

constrain(val, o, 255); // The analog to digital converter has value range 0 to 1023, PWM is only 0-255, so we constrain it

return val;

}

If you disconnect the motor currently connected to Motor A on the motor shield and connect the other motor to those terminals, does this also produce the same behaviour?

If you have questions about specific elements of the code, just ask. The goal of your university project is for you to learn, and I am willing to help you learn.

PeterH I assume so as Retroplayer is saying that the code tells me exactly what it is doing however I am modifying it as instructed by my tutor.

Retroplayer thanks for the code :slight_smile: but I'm not even experienced in changing code much as the code you've given just blew my mind. When I've finished modifying the code I've currently got from a friend in class would you mind having a look at the code and adding what is missing?

I am willing to help. I am not sure why your tutor is asking you to move around pins, as the code you posted is meant to do exactly what you were observing. So, you had it wired up correctly. Perhaps he was engaging you in troubleshooting to get you to learn?

I understand that you do not currently know how to write code, but I am assuming that is the point of your class (to learn how to write code.) I think if you look over my comments in the code carefully, you will understand it.

setup() is a function that is run only once on power up
loop() is a function that is run over and over again as long as power is applied
brake() is a function to stop your motors. I am not currently using it at all in my code, but it is a useful function for future exploration
getpulse() is a function that will read your sensor connected to the appropriate pin (hint)

Within each "function" are a set of instructions that are run when the function is "called". Setup and loop are called automatically at power up, so you do not call these directly. However, the rest you need to call in your code.

In loop() for example, you can see that I am calling getpulse() and assigning that to a value (val) as an integer (int). This will call the getpulse function and run the code within it. When it is done, val will equal the value of your sensor (0 to 255).

You said you are trying to run two motors, both in the same direction and control their speed by pressing harder on the sensor. That much is correct?

If you take my code, and go here: Arduino - Home and read what each instruction in my code does, it will help.

MrShazzyMan:
PeterH I assume

I thought the problem was that only one motor is working, and you wanted to find out why the other motor wasn't working. Is that not the case?

The working motor seems to be doing what you'd expect from the code you posted, but I don't see why the other motor isn't working. So I have suggested you swap the motors over to see whether it is a hardware problem with the motors themselves.

PeterH:
I thought the problem was that only one motor is working

Hmm, I missed that part. Good catch. MotorA IS behaving as the code instructed, but not MotorB. That would be an error in his wiring, a broken H-bridge channel, or a broken motor. Better get that sorted out, OP, or even my fixed code won't work.

Sorry, PeterH

Right so after talking to my other lecturer who is awesome at coding. He's managed to write up the code to make both motors spin one way. Turns out the wiring is perfectly fine. Now all I need to do is add the sensor to the project which will then somehow allow me to control the amount of speed that is given out.

Fix my code and it will do exactly that.

That's fine. I just need to use the code for the sensor right as I have the motors working perfectly fine now.