Move 2 Dc motors using Flex sensors

Hey everyone

I need some help
I have an Arduino Nano
2 1000 Pololu dc motors
2 bidirectional flex sensors
1 breadboard

So ive connected everything as shown in the attachment but i have no clue how to link the flex sensors to be able to control the dc motors

Any help is apreciated
Thanks

Here's OP's pic for simplicity.

(OP read how to attach an image for future reference.)

Then explain exactly what this means:

link the flex sensors to be able to control the dc motors

What do you want the sensors to do to or with the motors: what does control mean?

Have you read any tutorials on the sensors themselves (try the vendor's site) to get them working alone, perhaps just to Serial.print() their values?

Hi,
Before you try the flex sensors.
Do you have control of the motors?
Does your code work at this stage, before you add extra bits of hardware?

Can you please post a copy of your circuit as a picture of a hand drawn circuit in jpg, png?
Fritzy pictures tell us nothing, not pin labels or proper circuit symbols.

Tom... :slight_smile:

Link to the data sheet of the flex sensor you are looking at would also be helpful.

thank you all for the quick replies

to answer your questions i have only connected the the wiring i did not manage to move the motors as the link does not provide the coding and i cannot find it anywhere else online
i am hoping someone here can help out... ::slight_smile:

this is the link to the following diagram which gives the instructions of the wiring

Arduino Nano L293D Wiring Diagram

Here’s how to wire the Arduino Nano to an L293D motor controller. Connect the signal pins as follows. Then, wire the power, ground, and motor leads together using the diagram, below.

L293D Enable 1 to 3
L293D IN1 to 2
L293D IN2 to 4
L293D IN3 to 6
L293D IN4 to 7
L293D Enable 2 to 5

This is the L293D Dual Dc motor controller Pinout

L293D PINS.png

then these are the Pinouts for the Arduino Nano

here is the flex sensor and ive also attached the PDF Data Sheet

the battery that im using to power up is a 7.4v 2000mah Lipo battery
i am using 2 1000:1 Micro Metal Gearmotor LP 6V
General specifications
Gear ratio: 986.41:1
No-load speed @ 6V: 13 rpm
No-load current @ 6V: 0.02 A
Stall current @ 6V: 0.36 A3
Stall torque @ 6V: 5.5 kg·cm3
Extended motor shaft?: N
Motor type: 0.36A stall @ 6V LP 6V

so the idea is motors do not have to move at the same time but they have to move according to 2
bidirectional flex sensors clockwise and counter clockwise depending on which way the sensors are flexed

L293D PINS.png

flex22.pdf (329 KB)

Eastbandit23:
to answer your questions i have only connected the the wiring i did not manage to move the motors as the link does not provide the coding and i cannot find it anywhere else online

You don't have access to Google or another halfway decent search engine? This is such a common h-bridge there is lots of code out there.

Get your motors to move first. Learn how to control them (make them go faster/slower). When that's done start looking at your flex sensors.

Eastbandit23:
i did not manage to move the motors as the link does not provide the coding and i cannot find it anywhere else online

L293D Enable 1 to 3
L293D IN1 to 2
L293D IN2 to 4
L293D IN3 to 6
L293D IN4 to 7
L293D Enable 2 to 5

I searched "arduino L293 coding", got loads of hits with code, like this one or this one for example. (I haven't tested them, not vouching for them, just saying there's no shortage of code around. ).

But the ideas is that you make the "Enable" high to make either motor work, and put a combination of high/low or low/high on either motor's "IN" pins for direction. The second link I gave uses analogWrite on EN for speed control.

(Check the pin numbers before you start: may not be the same as yours, I didn't look.)

Get that working without worrying about the flex sensor for now, is my advice.

Meantime, get the sensor working by itself. The pdf you linked says this:

The impedance buffer in the [Basic Flex Sensor Circuit] (above) is a single sided operational amplifier, used with these sensors because the low bias current of the op amp reduces errer due to source impedance of the flex sensor as voltage divider. Suggested op amps are the LM358 or LM324

Which is a bit of a palaver.

But then it also says this:

You can also test your flex sensor using the simplest circut, and skip the op amp.

I’d start there, with just analog read of the output like a potentiometer; the very very first IDE example tells you how to do that and print the results.

Then, decide on the thresholds for your direction (and presumably stationary with the sensor in about the middle.) And create simple logic to write the L293 pins as low/high under some value, high/low over some other value (with enable high), and low on the enable between the values to stop.

as you probably guessed i am a beginner i thought i had to connect everything including the sensors before i get the motors moving
but i will take your advice and work on the motor coding to get them moving then come back here for the flex sensor

thanks for the support so far !

Eastbandit23:
i am a beginner i thought i had to connect everything including the sensors

Especially for a beginner that would be wrong. Even experienced folk, when encountering hardware they've never seen before, would very likely get them working separately.

If you don't do it that way, if it doesn't work the trouble shooting is more complex since you have at least 2 things to look at. You would then end up isolating the problem by taking things out, so you might as well have started that way!

sayHovis:
Especially for a beginner that would be wrong. Even experienced folk, when encountering hardware they've never seen before, would very likely get them working separately.

If you don't do it that way, if it doesn't work the trouble shooting is more complex since you have at least 2 things to look at. You would then end up isolating the problem by taking things out, so you might as well have started that way!

thank you for the advice i will look into it and come back !

Totally agreed: get everything working separately first (the motors, the individual sensors), and then add bits one by one. That way you know where to search for the problem if it stops working.

hey everyone!

so ive tried to move the motors without any success
i found this sketch alongside an example of an arduino Uno
and tried to make it compatible with the Nano

here are the connections of the arduino uno as found online

arduino uno dual dc L293D.jpg
and here is the code

//L293D
//Motor A
const int motorPin1 = 5; // Pin 14 of L293
const int motorPin2 = 6; // Pin 10 of L293
//Motor B
const int motorPin3 = 10; // Pin 7 of L293
const int motorPin4 = 9; // Pin 2 of L293

//This will run only one time.
void setup(){

//Set pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);

//Motor Control - Motor A: motorPin1,motorpin2 & Motor B: motorpin3,motorpin4

//This code will turn Motor A clockwise for 2 sec.
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(2000);
//This code will turn Motor A counter-clockwise for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(2000);

//This code will turn Motor B clockwise for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(2000);
//This code will turn Motor B counter-clockwise for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(2000);

//And this code will stop motors
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);

}

void loop(){

}

any idea of what modifications should be done to this code in order to work with these connections below?

arduino uno dual dc L293D.jpg

Do post a proper schematic instead of such a completely unreadable spaghetti diagram.

Don't use a breadboard with motors; a breadboard can't handle the current.

Go back to first principles: don’t try and fudge one circuit to fit another.

The diagram below shows how to connect 2 motors to an L293. All you need to do is make sure that whatever pins you decide to use on the Arduino are addressed accordingly in the code. Make sure (wvmarle’s reservations about breadboard notwithstanding) that you layout your circuit to match the pins.

Here’s in words, what the L293 expects:

  • At least one of the 4 grounds in the middle must go to Arduino ground
  • The voltage labelled 9V is of course whatever voltage the motors need, your battery
  • The 5V logic on the L293 can come from the Arduino 5V: this is the power that actually “runs” the 293
  • Then the left side of the 293 has 3 input pins for its motor: the 1-2EN enable (see note * below) and the 1A and 2A direction pins. Those come from Arduino outputs of your choice.
  • The left hand motor is then connected to the 293’s outputs, 1Y and 2Y
  • Ditto the right hand side motor

2dc motors L293.png

  • Note: the two Fritzings you presented differ in an important way. In the upper one, the L293 EN pins are hardwired to 5V so the 2 motors are always enabled. Since those pins do not connect to digital pins on the Arduino, there is no need for a digitalWrite() or analogWrite() to the EN pins. There are only 4 lines from Arduino outputs to the L293. In the lower diagram I think (it’s very messy :wink: ) there are 6 lines: the 2 EN pins on the L293 are thus under the control of the Arduino. That one is in line with the schematic I posted above. If you go that way, you need to make those 2 extra pins outputs on the Arduino, and ENable and disable either side of the 293 under program control.

The advantage of doing it that way is that you can use analogWrite() to control the motor speed, provided you use a PWM ~ pin.

edit… To be more explicit re your question:

any idea of what modifications should be done to this code in order to work with these connections below?

To use the code for the upper Fritzing with the lower one:

  • Declare the Arduino pins which go to the EN pins, in a way similar to the way the other pins are
  • pinMode() them as outputs
  • digitalWrite() them as high.

further edit: Note that the code you presented there has everything in setup() which means the motors will just run through one cycle. Fine for a test of the wiring, but not much use irl. You will need to move into loop() later when you know that works and when you have your sensors working, so that the sensors control the motors on the fly. We’ll get to that ;).

i have moved the motors they are connected on a breadboard
so basically they look like the image below

i have used this demo code i have found on the web here it is

// L293D

//Define Pins
//Motor A
int enableA = 3;
int MotorA1 = 2;
int MotorA2 = 4;

//Motor B
int enableB = 5;
int MotorB1 = 6;
int MotorB2 = 7;

void setup() {

Serial.begin (9600);
//configure pin modes
pinMode (enableA, OUTPUT);
pinMode (MotorA1, OUTPUT);
pinMode (MotorA2, OUTPUT);

pinMode (enableB, OUTPUT);
pinMode (MotorB1, OUTPUT);
pinMode (MotorB2, OUTPUT);

}

void loop() {
//enabling motor A
Serial.println ("Enabling Motors");
digitalWrite (enableA, HIGH);
digitalWrite (enableB, HIGH);
delay (1000);
//do something

Serial.println ("Motion Forward");
digitalWrite (MotorA1, LOW);
digitalWrite (MotorA2, HIGH);

digitalWrite (MotorB1, LOW);
digitalWrite (MotorB2, HIGH);

//3s forward
delay (3000);

Serial.println ("Motion Backwards");
//reverse
digitalWrite (MotorA1,HIGH);
digitalWrite (MotorA2,LOW);

digitalWrite (MotorB1,HIGH);
digitalWrite (MotorB2,LOW);

//5s backwards
delay (3000);

Serial.println ("Stoping motors");
//stop
digitalWrite (enableA, LOW);
digitalWrite (enableB, LOW);
delay (3000);
}

now that i managed to move the motors
i dont quite understand how do i make the motors move faster or slower
in the demo code i used there aren't really any values i could input for the RPM
im accepting any advice

also perhaps someone could show me where i could link the flex sensors on the diagram in order
to control the motors...

thank you all very much

now that i managed to move the motors
i dont quite understand how do i make the motors move faster or slower

To vary the speed (not with the flex sensors yet, we’ll get back to that :wink: ) you need to swop these digitalWrite()s:

digitalWrite (enableA, HIGH);
digitalWrite (enableB, HIGH);

… for analogWrite()s:

analogWrite (enableA, <a number from 0 to 255>);
analogWrite (enableB, <a number from 0 to 255>);

… where 0 will be stopped and 255 will be same as a digitalWrite (enableA, HIGH).

So for now you can just hardcode a value in and see what it does.

You could actually duplicate all the code that you have inside loop just below itself in loop and have each block giving a different speed.

You may find that with lower values you don’t get any movement. An analogWrite() effectively reduces the average voltage to the motor, and sometimes a low but non-zero value is too low to work.

also perhaps someone could show me where i could link the flex sensors on the diagram in order
to control the motors…

Have you got those working by themselves yet in their own sketch to serial print their values?

First of all, do go and read the forum sticky, especially on how to properly post code between code tags.

Eastbandit23:
now that i managed to move the motors

That's a great start!

i dont quite understand how do i make the motors move faster or slower
in the demo code i used there aren't really any values i could input for the RPM

Use analogWrite(), using PWM to regulate the power to the motors.

Note: you can NOT set the rpm directly. You're regulating the power to the motors. More power means they run faster, how much faster depends on the load etc. RPM control is a lot more complex, as you need speed feedback from your motor and adjust the PWM duty cycle (the analogWrite() value) accordingly.

Okay so i have swapped digitalWrite with AnalogWrite
i played around with the speed
i have hooked up my bidirectional flex sensor
Data pin to A0

  • to 5v
    Gnd to Gnd with a 10kOHM resistor
    i havent found any good codes online
    how can i implement my current code for the flex sensor to work as a switch
    and move the dc motor backwards and forwards as the flex is bent...

thank you all for the support

Find the readings of your flex sensor as you bend them.

Use if statements to set the direction of the motors based on it.