RyanBowen2010
Posts: 8
Joined: Mon Jan 20, 2014 6:52 pm

L298N Motor driver help

Thu Feb 06, 2014 6:25 pm

Okay so I bought this L298N,
http://www.facebook.com/l.php?u=http%3A ... h=3AQGmRfC_

and I hooked it up to an RC car which has 2 motors (to the out1,out2,out3 & out4) But I have these 3 terminal blocks;
VCC, GND, 5v

Do I need to connect a power supply to VCC and 5v to the 5v terminal?

If anyone has a schematic it would be awesome. Thanks

Edit: Just read that the VCC is the motor power (up to like 35v, so if I'm using a 3v/6v motor can I use a 9v battery? will it regulate the voltage) and the 5v terminal block is the logic power supply (Yet again, must it be exactly 5v? I only have 3.5v pack)
Last edited by RyanBowen2010 on Thu Feb 06, 2014 6:31 pm, edited 1 time in total.

User avatar
mikronauts
Posts: 2637
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: L298N Motor driver help

Thu Feb 06, 2014 6:31 pm

Connect the GND on the Pi to the terminal block

DO NOT CONNECT 5v/VCC TO THE PI!

VCC is presumably for your battery (to power the motors); do not use the same battery to power the Pi and the motors

5V on the L298 is probably 5V out from an on-board regulator. You will probably need 7V from the motor batteries as the 7805 needs at least 6.2V, and your module may have a diode after Vcc.

I've just been building a Pi robot with an L298 driver board, Model B rev2 Pi and my RoboPi controller. I am using the L298 to power two gear motors on each channel (800mA stall current per motor, so 1.6A max for two motors on one L298 channel) as I am using a 4WD chassis.
RyanBowen2010 wrote:Okay so I bought this L298N,
http://www.facebook.com/l.php?u=http%3A ... h=3AQGmRfC_

and I hooked it up to an RC car which has 2 motors (to the out1,out2,out3 & out4) But I have these 3 terminal blocks;
VCC, GND, 5v

Do I need to connect a power supply to VCC and 5v to the 5v terminal?

If anyone has a schematic it would be awesome. Thanks
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

RyanBowen2010
Posts: 8
Joined: Mon Jan 20, 2014 6:52 pm

Re: L298N Motor driver help

Thu Feb 06, 2014 6:36 pm

mikronauts wrote:Connect the GND on the Pi to the terminal block

DO NOT CONNECT 5v/VCC TO THE PI!


RyanBowen2010 wrote:Okay so I bought this L298N,
http://www.facebook.com/l.php?u=http%3A ... h=3AQGmRfC_

and I hooked it up to an RC car which has 2 motors (to the out1,out2,out3 & out4) But I have these 3 terminal blocks;
VCC, GND, 5v

Do I need to connect a power supply to VCC and 5v to the 5v terminal?

If anyone has a schematic it would be awesome. Thanks
I haven't done anything with it yet. Just making sure first. I have a 9v battery for VCC, what do you mean I can't connect it to the Pi, so I can't use this 5v and GND on the pi for the logic gate power? :/ I really need a schematic.

Image
See, it's the +5v input, used for the logic gates and stuff. Why can't I connect it to the pi?
Last edited by RyanBowen2010 on Thu Feb 06, 2014 6:39 pm, edited 1 time in total.

User avatar
mikronauts
Posts: 2637
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: L298N Motor driver help

Thu Feb 06, 2014 6:38 pm

Use 6xAA NimH or 7.2V-9V RC car battery pack for best results, you'll drain a 9V battery very very quickly.

Use one of those "USB chargers" to power the Pi (2.1A or higher output), I tend to use the Patriot Fuel+ 5200mAh as it works great for me.

You might want to get Servo 2014, my SPRITE article has a lot of info you would find useful.
RyanBowen2010 wrote: Edit: Just read that the VCC is the motor power (up to like 35v, so if I'm using a 3v/6v motor can I use a 9v battery? will it regulate the voltage) and the 5v terminal block is the logic power supply (Yet again, must it be exactly 5v? I only have 3.5v pack)
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
mikronauts
Posts: 2637
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: L298N Motor driver help

Thu Feb 06, 2014 6:41 pm

Sorry, I don't have time to draw one (am taking a quick break from working on documentation)

Basically, use a 7.2V-9V battery pack, + from it to VCC on the L298 board. - to GND. GND also goes to Pi's GND. Be careful to get the polarity right. IN1-4 to Pi GPIO's.

Power the Pi from a USB battery pack.

Use the "WiringPi" or "PIGPIO" libraries for PWM.
RyanBowen2010 wrote:
mikronauts wrote:Connect the GND on the Pi to the terminal block

DO NOT CONNECT 5v/VCC TO THE PI!
RyanBowen2010 wrote:Okay so I bought this L298N,
http://www.facebook.com/l.php?u=http%3A ... h=3AQGmRfC_

and I hooked it up to an RC car which has 2 motors (to the out1,out2,out3 & out4) But I have these 3 terminal blocks;
VCC, GND, 5v

Do I need to connect a power supply to VCC and 5v to the 5v terminal?

If anyone has a schematic it would be awesome. Thanks
I haven't done anything with it yet. Just making sure first. I have a 9v battery for VCC, what do you mean I can't connect it to the Pi, so I can't use this 5v and GND on the pi for the logic gate power? :/ I really need a schematic.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

RyanBowen2010
Posts: 8
Joined: Mon Jan 20, 2014 6:52 pm

Re: L298N Motor driver help

Thu Feb 06, 2014 6:48 pm

So I just leave the 5v terminal block? I found diagrams that tell me that I need to use it..
Image :/

User avatar
joan
Posts: 13556
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: L298N Motor driver help

Thu Feb 06, 2014 8:27 pm

You should remove the jumper labelled 5V enable if you are powering the motors with much more than 6V. If you remove the jumper then supply logic power from the Pi's 5V pin.

Personally I'd leave jumpers over ENA and ENB. Plug the motor power positive in to the 12V terminal, motor power ground into the ground terminal, Pi ground into the ground terminal and Pi 5V into the 5V logic terminal (if you have removed the 5V enable jumper). Then connect any gpios you fancy to IN1,2,3,4.

At the end of the day though it's your Pi, your motor controller, and your responsibility.
Last edited by joan on Fri Feb 07, 2014 10:10 am, edited 1 time in total.

Shea
Posts: 104
Joined: Fri Nov 25, 2011 7:16 pm
Location: Markham, Canada

Re: L298N Motor driver help

Thu Feb 06, 2014 9:54 pm

Hi, what are you try to do with the L298N chip. The same question has came up many times in the forum. I do not know where the diagram come from, but you need to put the documentation with diagram in the same message to indicate what is the reasoning behind the diagram.
The 5V out has a max current of .5A, so it does not work with the pi in most case. If you want to try to power the pi with 5V out, then you need at least 6V, or even better with a 7.2V battery.

elmsley
Posts: 17
Joined: Tue Feb 05, 2013 3:23 pm

Re: L298N Motor driver help

Tue Jul 22, 2014 9:11 pm

I've seen many posts about the L298N, and I'm probably in the same boat as a lot of people. Is there no way to use the RPi to control two motors off this chip?

If it's recommended to use a different chip, then some recommendations/instructions would be great.

However if there is a way, I'd like to know. My setup somehow works but only one motor is running at a time, I'm trying to figure out why both don't work together. If I'm at risk of frying this thing, I'll stop and wait for some expert advice.

User avatar
joan
Posts: 13556
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: L298N Motor driver help

Tue Jul 22, 2014 9:24 pm

Chip or motor driver board?

Duane Degn
Posts: 55
Joined: Thu Jul 10, 2014 6:30 am
Location: Idaho, USA

Re: L298N Motor driver help

Tue Jul 22, 2014 9:28 pm

Technically, the L298N should be powered with at least 5.8V when used with 3.3V logic on the IN1 through IN4 pins.

I made this video to help someone use one of these h-bridges.

As you'll see from the end of the video, the L298N works well with 3.3V logic.

I agree with the warnings about powering the Pi from the L298N board's 5V regulator. The regulator can provide enough current for the microcontroller I used in the video, but it's not likely a good match for the Raspberry Pi.

In the video I used a battery pack with only 5.3V. I used since it had the right sort of connectors but I think in generally, you'd be better off with somewhere between 6V and 14V (depending on the motors you use).

elmsley
Posts: 17
Joined: Tue Feb 05, 2013 3:23 pm

Re: L298N Motor driver help

Tue Jul 22, 2014 11:10 pm

Sorry motor driver board. This one but red.

http://www.seeedstudio.com/depot/images ... robot5.jpg

I'm completely new to all this, so excuse my lack of knowledge. I didn't even know there was a chip version.

elmsley
Posts: 17
Joined: Tue Feb 05, 2013 3:23 pm

Re: L298N Motor driver help

Tue Jul 22, 2014 11:13 pm

Yeah I'm looking something like this video for RPi.. and maybe with python logic for two motors. I have code for one motor which I essentially duplicated, but I can't seem to turn two motors on at the same time. The L298N seems to be blocking it from starting them both up.

Actually I thought I didn't have enough V so I switched my 6V to a 12V but that gave the same results.
Duane Degn wrote:Technically, the L298N should be powered with at least 5.8V when used with 3.3V logic on the IN1 through IN4 pins.

I made this video to help someone use one of these h-bridges.

As you'll see from the end of the video, the L298N works well with 3.3V logic.

I agree with the warnings about powering the Pi from the L298N board's 5V regulator. The regulator can provide enough current for the microcontroller I used in the video, but it's not likely a good match for the Raspberry Pi.

In the video I used a battery pack with only 5.3V. I used since it had the right sort of connectors but I think in generally, you'd be better off with somewhere between 6V and 14V (depending on the motors you use).

Duane Degn
Posts: 55
Joined: Thu Jul 10, 2014 6:30 am
Location: Idaho, USA

Re: L298N Motor driver help

Wed Jul 23, 2014 2:18 am

elmsley wrote:Sorry motor driver board. This one but red.

http://www.seeedstudio.com/depot/images ... robot5.jpg
The L298 motor controller comes in several different "packages" or chip shapes. Some are intended to be mounted flush with the PCB (L298P). Some (it would appear most) are mounted vertically (L298N). There are also some mounted horizontally (L298HN). The L298N can be forced into a breadboards but it's not pretty.

Image

Each motor should have four flyback diodes which makes using the L298N on a breadboard extra challenging (the above photo has two L298N controllers in it).

There are many different L298N boards. It's important to know which kind you have. When you say "This one but red" does that mean it's exactly the same configuration? Some L298N board come with inverters to make it easier to control a motor with only two pins. Many (most?) have pins which can be used with jumpers. Many L298N boards include a 5V pin next to the enable pins to make it easy to use the controller with stepper motors. If you don't understand which pins do what it can have disastrous consequences.

Is the only difference between your board and the Seeed Studio board the color?

Duane Degn
Posts: 55
Joined: Thu Jul 10, 2014 6:30 am
Location: Idaho, USA

Re: L298N Motor driver help

Wed Jul 23, 2014 3:39 am

That's one bizarre [urlhttp://www.seeedstudio.com/depot/L298-Dual-HBr ... Path=91_92]L298N being sold at the Seed Studio Bazaar[/url].

Some of the images have the motor outputs labeled "GND" and VCC" which is wrong in so many ways. You do NOT want to connect the motor output to ground.

There are some other strange things but I don't think any of the boards peculiarities would prevent it from working with the Raspberry Pi.

Do you have a multimeter? It would be nice if you could double check how things on the boards are connected. It doesn't look like there's an easy way to pull the enable pins high. This seems strange since there's a stepper motor connector on the back end of the board. The four pin stepper motor connector is an alternate way of connecting to the h-bridges' output.

One L298N can drive four independent DC motors if the motors don't need to reverse. If the DC motors do need to be reversible then it can only drive two of them. One L298N can also drive a single stepper motor (bi-polar and I think mono-polar).

When used to drive a stepper motor the enable pins are generally held high. Since you want to control 2 two direction DC motors, you don't really need to worry about tying the enable pin high be it seems strange the ability to do so isn't on a board with so many jumpers.

If your board really is like the Seeed Studio one, let me know and I (and likely others) will try to help you safely connect it to your Pi. don't just start plugging things in without knowing what you're doing. You could easily damage/destroy your Pi. There have been may Raspberry Pi boards done in by connecting them incorrectly to L298N boards.

elmsley
Posts: 17
Joined: Tue Feb 05, 2013 3:23 pm

Re: L298N Motor driver help

Wed Jul 23, 2014 5:20 pm

It's this one exactly. Well not that specific one, but you wouldn't be able to tell the difference.

http://www.openhardware.pe/store/image/ ... 00x500.jpg

Duane Degn
Posts: 55
Joined: Thu Jul 10, 2014 6:30 am
Location: Idaho, USA

Re: L298N Motor driver help

Wed Jul 23, 2014 5:47 pm

elmsley wrote:It's this one exactly. Well not that specific one, but you wouldn't be able to tell the difference.

http://www.openhardware.pe/store/image/ ... 00x500.jpg
Very good. Then the video I linked to earlier explains which pin is for what.

You want to pulse the ENA and ENB pins with PWM to control speed and use the "IN" pins to set direction.

I think I have code to do this but I'm not adept enough with the Pi to share my Pi files with my PC.

I imagine there are lots of examples on the forum. I just don't know where they are.

If no one else chimes in with some code, I'll figure out how to cut and paste the code myself (I'll figure this out no matter but I'll try to figure it out sooner than later if no one else has some example code).

User avatar
joan
Posts: 13556
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: L298N Motor driver help

Wed Jul 23, 2014 6:03 pm

Alternatively you could just jumper ENA/B high and use PWM to control speed on the IN pins instead. That way only requires 2 gpios per motor.

Duane Degn
Posts: 55
Joined: Thu Jul 10, 2014 6:30 am
Location: Idaho, USA

Re: L298N Motor driver help

Wed Jul 23, 2014 8:25 pm

joan wrote:Alternatively you could just jumper ENA/B high and use PWM to control speed on the IN pins instead. That way only requires 2 gpios per motor.
That works but it can sound awful as the motor does a "fast brake" with each low pulse. Apparently at high PWM frequencies (which don't work well on the L298N) using pulse/brake may have some advantages (though I'm not convinced).

In my experience, using pulse/brake uses more current than pulse/coast (pulsing the enable pins) and I'm not sure, but I think the h-bridge gets hotter when using pulse/brake.

I'm gathering some equipment in hopes of getting some hard numbers using the two techniques. I encourage anyone interested to try the two different techniques with their motors/gearboxes to see if they can tell the difference. When I used some nice 12V gear motors from Pololu, the difference was dramatic (at 200Hz).

One can still use two pins to control the motor by using an inverter on one of the IN pins (at the cost of the ability to fast brake).

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: L298N Motor driver help

Thu Jul 24, 2014 7:39 am

I wrote some code for using the L298, unfortunately bitbucket stopped updating from the adafruit WebIDE so this is quite old.

much of this code relates to using the i2c based servo driver I am using to provide PWM to the L298N

Code: Select all

#!/usr/bin/python

from Adafruit_PWM_Servo_Driver import PWM
import time

# ===========================================================================
# Example Code
# ===========================================================================

# Initialise the PWM device using the default address
# bmp = PWM(0x40, debug=True)
pwm = PWM(0x40, debug=True)

servoMin = 150  # Min pulse length out of 4096
servoMax = 600  # Max pulse length out of 4096

def setServoPulse(channel, pulse):
  pulseLength = 1000000                   # 1,000,000 us per second
  pulseLength /= 60                       # 60 Hz
  print "%d us per period" % pulseLength
  pulseLength /= 4096                     # 12 bits of resolution
  print "%d us per bit" % pulseLength
  pulse *= 1000
  pulse /= pulseLength
  pwm.setPWM(channel, 0, pulse)


def forward():
    print "forward"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 0, 4095)
    pwm.setPWM(2, 0, 4095)
    pwm.setPWM(3, 0, 0)
  
def backward():
    print "backward"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 4095)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 0, 4095)
  
def stop():
    print "stop"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 0, 0)
  
def turnRight():
    print "turnRight"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 0, 4095)
    pwm.setPWM(3, 0, 0)
  
def turnLeft():
    print "turnLeft"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 0, 4095)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 0, 0)

  
def spinRight():
    print "spinRight"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 4095)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 0, 4095)
    pwm.setPWM(3, 0, 0)
    
def spinLeft():
    print "spinLeft"
    pwm.setPWMFreq(50)                        # Set frequency to 50 Hz
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 0, 4095)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 0, 4095)

#this section was eventually replaced by a keyboard command set, but works for starter to test various motions.
forward()
time.sleep(10)
stop()
# time.sleep(10)
# backward()
# time.sleep(10)
# stop()
# time.sleep(10)
# turnLeft()
# time.sleep(10)
# stop()
# time.sleep(10)
# turnRight()
# time.sleep(10)
# stop()
# time.sleep(10)
# spinLeft()
# time.sleep(10)
# stop()
# time.sleep(10)
# spinRight()
# time.sleep(10)
# stop()
Doug.
Building Management Systems Engineer.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: L298N Motor driver help

Thu Jul 24, 2014 7:59 am

If you took this as a template and added your chosen method of controlling your GPIO it should work, you could then develop further once this is working.

Code: Select all

#!/usr/bin/python

import time
import RPi.GPIO

# ===========================================================================
# Example Code
# ===========================================================================

# L298N setup code

# Define Outputs to L298N 
# enA =
# in1 =
# in2 =
# enB =
# in3 =
# in4 =


def forward():
    print "forward"
    # set enA on, set in1 on and in2 off
    # set enB on, set in3 on and in4 off
  
def backward():
    print "backward"
    # set enA on, set in1 off and in2 on
    # set enB on, set in3 off and in4 on
  
def stop():
    print "stop"
    # Motor breaking
    # set enA on, set in1 off and in2 off
    # set enB on, set in3 off and in4 off

    # coasting
    # set enA off, set in1 off and in2 off
    # set enB off, set in3 off and in4 off
  
def turnRight():
    print "turnRight"
    # set enA on, set in1 on and in2 off
    # set enB off, set in3 off and in4 off
  
def turnLeft():
    print "turnLeft"
    # set enA off, set in1 off and in2 off
    # set enB on, set in3 on and in4 off

  
def spinRight():
    print "spinRight"
    # set enA on, set in1 on and in2 off
    # set enB on, set in3 off and in4 on
    
def spinLeft():
    print "spinLeft"
    # set enA on, set in1 off and in2 on
    # set enB on, set in3 on and in4 off

forward()
time.sleep(10)
stop()
time.sleep(10)
backward()
time.sleep(10)
stop()
time.sleep(10)
turnLeft()
time.sleep(10)
stop()
time.sleep(10)
turnRight()
time.sleep(10)
stop()
time.sleep(10)
spinLeft()
time.sleep(10)
stop()
time.sleep(10)
spinRight()
time.sleep(10)
stop()
Doug.
Building Management Systems Engineer.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: L298N Motor driver help

Thu Jul 24, 2014 8:11 am

aha, found the code I last used, this will drive the motors from a keyboard (I connect by adafruit webIDE via WIFI).

I'm still planning on accessing these commands by sockets in the future, I'll probably have to remove webIDE to install apache, then re-install webIDE afterwards, so if you are planning on using webIDE make sure you have installed apache first (and follow the alternate instructions for installing webIDE as a secondary interface).

to use this code without the PWM servo driver just change the code in each of the definitions to your desired GPIO command code (as posted previously).

Code: Select all

from Adafruit_PWM_Servo_Driver import PWM
import sys, tty, termios, time


# The getch method can determine which key has been pressed
# by the user on the keyboard by accessing the system files
# It will then return the pressed key as a variable
def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

pwm = PWM(0x40, debug=True)

servoMin = 150  # Min pulse length out of 4096
servoMax = 600  # Max pulse length out of 4096

def setServoPulse(channel, pulse):
  pulseLength = 1000000                   # 1,000,000 us per second
  pulseLength /= 60                       # 60 Hz
  print "%d us per period" % pulseLength
  pulseLength /= 4096                     # 12 bits of resolution
  print "%d us per bit" % pulseLength
  pulse *= 1000
  pulse /= pulseLength
  pwm.setPWM(channel, 0, pulse)

def forward():
    print "forward"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 2000, 4000)
    pwm.setPWM(2, 2000, 4000)
    pwm.setPWM(3, 0, 0)
  
def backward():
    print "backward"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 2000, 4000)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 2000, 4000)
  
def stop():
    print "stop"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 0, 0)
  
def turnLeft():
    print "turnLeft"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 2000, 4000)
    pwm.setPWM(3, 0, 0)
  
def turnRight():
    print "turnRight"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 2000, 4000)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 0, 0)

  
def spinLeft():
    print "spinLeft"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 2000, 4000)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 2000, 4000)
    pwm.setPWM(3, 0, 0)
    
def spinRight():
    print "spinRight"
    pwm.setPWMFreq(50)                        # Set frequency to 50 Hz
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 2000, 4000)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 2000, 4000)

def reverseLeft():
    print "reverseLeft"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 2000, 4000)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 0, 0)

def reverseRight():
    print "reverseRight"
    pwm.setPWMFreq(50)
    pwm.setPWM(0, 0, 0)
    pwm.setPWM(1, 0, 0)
    pwm.setPWM(2, 0, 0)
    pwm.setPWM(3, 2000, 4000)
    
# Setting the PWM pins to false so the motors will not move
# until the user presses the first key
pwm.setPWM(0, 0, 0)
pwm.setPWM(1, 0, 0)
pwm.setPWM(2, 0, 0)
pwm.setPWM(3, 0, 0)

# Instructions for when the user has an interface
print("w/x: acceleration")
print("q/e, a/d, z/c: steering")
print("m: exit")

# Infinite loop that will not end until the user presses the
# exit key
while True:
    # Keyboard character retrieval method is called and saved
    # into variable
    char = getch()

    # The car will drive forward when the "w" key is pressed
    if(char == "w"):
        forward()

    # The car will reverse when the "x" key is pressed
    if(char == "x"):
        backward()
        
    # The "q" key will toggle the steering left
    if(char == "q"):    
        turnLeft()

    # The "e" key will toggle the steering right
    if(char == "e"):
        turnRight()
        
    # The "a" key will toggle the steering left
    if(char == "a"):
        spinLeft()

    # The "d" key will toggle the steering right
    if(char == "d"):
        spinRight()
    
    if(char == "z"):
        reverseLeft()
    
    if(char == "c"):
        reverseRight()

    # The "s" key will stop the motor
    if(char == "s"):
        stop()

    # The "m" key will break the loop and exit the program
    if(char == "m"):
        print("Program Ended")
        break

    # At the end of each loop the acceleration motor will stop
    # and wait for its next command
    stop()

    # The keyboard character variable will be set to blank, ready
    # to save the next key that is pressed
    char = ""

# Program will cease all GPIO activity before terminating
Doug.
Building Management Systems Engineer.

nelu881
Posts: 5
Joined: Fri Nov 07, 2014 10:44 am

Re: L298N Motor driver help

Thu Dec 04, 2014 4:23 pm

i need help ...when I power on raspberry, 2 motors connected to port A from L298N is switched itself without any commands .. why... maybe l298n is defect or raspberry..

User avatar
joan
Posts: 13556
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: L298N Motor driver help

Thu Dec 04, 2014 4:27 pm

nelu881 wrote:i need help ...when I power on raspberry, 2 motors connected to port A from L298N is switched itself without any commands .. why... maybe l298n is defect or raspberry..
Is this a motor driver board?

Do you have a link to the model you are using?

What wires have you connected between the Pi and the L298N?

A close-up photo showing the connections may help.

nelu881
Posts: 5
Joined: Fri Nov 07, 2014 10:44 am

Re: L298N Motor driver help

Thu Dec 04, 2014 5:51 pm

the model of l298n is from this link http://www.ebay.com/itm/NEW-L298N-Stepp ... 259a7e6437

2 motor dc are connected to output A
2 motor dc are connected to output B
+7v baterry a connected to + to VCC and - to GND
from rasp gpio pin 2 are connected to +5 power l298 and pin 6 to GND, and gpio 4 to input IN1, gpio 17 to input IN2,gpio 21 to input IN3,gpio 22 to input IN4..

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 20 guests