Go Down

Topic: Code for H-Bridge working in Proteus but not in real life (Arduino MEGA) (Read 1 time) previous topic - next topic

SimpleThings


Header file:
http://textuploader.com/ddh93

.cpp file:
http://textuploader.com/ddh9i

Okay guys I am trying to get working code for 2 H-bridges (L293d) controlled with 74HC_HCT595 shift register

http://www.nxp.com/documents/data_sheet/74HC_HCT595.pdf

Code: [Select]

#include <AFMotor.h>


int main(void)
{
  AF_DCMotor motor_1(1);
  AF_DCMotor motor_2(2);
  AF_DCMotor motor_3(3);
  AF_DCMotor motor_4(4);
 
  motor_1.setSpeed(200);
  motor_2.setSpeed(200);
  motor_3.setSpeed(30);
  motor_4.setSpeed(30);
    while (1)
    {
    motor_1.run(FORWARD);
    motor_2.run(FORWARD);
    }
}


I've found the this schematics on the internet:

https://cdn-learn.adafruit.com/assets/assets/000/009/769/original/mshieldv1-schem.png

and made my connections using the proteus picture in the attachment.

I've tried to measure voltages on the motor outputs and it shows 0 volts.
It is very confusing that it is working in the proteus but not in the real life.



SimpleThings

It seems that there is a problem with
Code: [Select]



// STCP Pin 12 Storage Register Clock Input
#define LATCH 4
#define LATCH_DDR DDRB
#define LATCH_PORT PORTB

// SHCP Pin 11 Shift Register Clock Input
#define CLK_PORT PORTD
#define CLK_DDR DDRD
#define CLK 4

// OE Pin 13 Output enable input (Aktivan na LOW)
#define ENABLE_PORT PORTD
#define ENABLE_DDR DDRD
#define ENABLE 7

// DS Pin 14 Serial Data Input
#define DATA 0                 
#define DATA_DDR DDRB
#define DATA_PORT PORTB



Could someone provide a schematics for the Arduino connections to SHIFT REGISTER?

Robin2

I reckon you will get advice much more quickly if you make a simple drawing showing how you have everything connected and post a photo of your drawing.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

SimpleThings

I reckon you will get advice much more quickly if you make a simple drawing showing how you have everything connected and post a photo of your drawing.

...R
Well I have used the already built Motor driver:

http://www.ebay.com/itm/Arduino-L293D-Expansion-Board-Professional-DC-Stepper-Motor-Driver-Module-/112008259221?hash=item1a14366695:g:F9EAAOSwBP9UXIhk

So basically I wasn't connecting anything together.
Of course one could tell why don't you take out drivers and shift register out and put them on the breadboard.
Well this shield has already built in noise capacitors as shown here
https://cdn-learn.adafruit.com/assets/assets/000/009/769/original/mshieldv1-schem.png
so it this shield is really practical.

When I work directly with registers DDRn PORTn for controlling shift register motors do not run.  But it works well using already written functions such us pinMode and digitalWrite and that doesnt make any sense to me.

This is the source code:
https://github.com/adafruit/Adafruit-Motor-Shield-library/blob/master/AFMotor.cpp
https://github.com/adafruit/Adafruit-Motor-Shield-library/blob/master/AFMotor.h

They use following pins

In the header file lines 120-134
Code: [Select]

#define LATCH 4
#define LATCH_DDR DDRB
#define LATCH_PORT PORTB
#define CLK_PORT PORTD
#define CLK_DDR DDRD
#define CLK 4
#define ENABLE_PORT PORTD
#define ENABLE_DDR DDRD
#define ENABLE 7
#define SER 0
#define SER_DDR DDRB
#define SER_PORT PORTB



But those pins are wrong,t so I changed them according to the datasheet pinout.

Digital pin 12 --> PB6
Digital pin 4   --> PG5
Digital pin 7  -->  PH4
Digital pin 8  -->  PH5

From this code I figured out that they are using those pins for controlling shift register since it's hard to see lines on PCB.

Code: [Select]

#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8


and

Code: [Select]

pinMode(MOTORLATCH, OUTPUT);
pinMode(MOTORENABLE, OUTPUT);
pinMode(MOTORDATA, OUTPUT);
pinMode(MOTORCLK, OUTPUT);



Robin2

One knows this is a super-professional project when one reads "chip every road and bridge" on the website :)

Is there any reason to conclude that the datasheet you found is identical to the physical product you have?

My suggestion is to buy a product that comes with proper documentation and some example programs.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

SimpleThings

Okay I've debugged it, for making ti simple I connected 8 LEDs to shift registers pins 16 - 9
and was changing pins accordingly to the table that I made.

So if anyone bought this item it could be of use:

https://drive.google.com/open?id=0BzNMcs6QcbfMRnhyWDd4TXo1QVU


http://www.ti.com/lit/ds/symlink/sn74hc595.pdf

This shield is really badly documented.

Robin2

This shield is really badly documented.
Glad to hear you have figured it out.

Perhaps you could write a short tutorial for the benefit of others?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

SimpleThings

Of course I could just tell me under what forum section should I upload it?

Robin2

Of course I could just tell me under what forum section should I upload it?
Just post it as a Reply in this Thread and keep a bookmark so you can refer others to it.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

SimpleThings

Also this shield shows some really weird behaviour:

Connected 4 toy DC motors:
 http://www.ebay.com/itm/Smart-Car-Robot-Plastic-Tire-Wheel-with-DC-3-6V-Gear-Motor-for-Robot-65-27MM/191901698035?_trksid=p2047675.c100005.m1851&_trkparms=aid%3D222007%26algo%3DSIC.MBE%26ao%3D2%26asc%3D38530%26meid%3D2ec27ce71aba4d4f870910021c8287e2%26pid%3D100005%26rk%3D2%26rkt%3D6%26sd%3D142102137189

Arduino connected to the IR receiver via interrupt pin+ motorshield controlling 4 DC motors

1) Shield only supplied with power via USB

Max 2 motors start rotating slowly and after few seconds usb port gets disconnected from the PC
Keys presses get received.

2) Shield only supplied with external DC power supply 8 VDC, 600 mADC


Not a single key press gets printed on the screen from arduino.
Motors try to move in a really short time ~0.1 s and after that they stop.


3) Shield supplied with both external DC power supply 8 VDC, 600 mADC + USB connected to the PC

Key pressed gets received.
Motors working properly.



Go Up