User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 572
Joined: Sat Nov 02, 2013 2:06 pm

PWM Motor Control Pi Arduino L298N i2C

Sun Jun 28, 2015 1:23 pm

Hi;

viewtopic.php?f=44&t=114266&p=781376#p781376

I have connected my Model B Rev 2 that I have successfully connected to an Arduino Nano ATMega328 via i2c.
Using information from several sites I have set the Arduino as a Slave and have a Python Code to turn on and off the onboard LED on the Nano.

I now want to turn on and off a Motor connected via an L298N Motor Driver Board (See guide linked below) but this is where I am stuck.
According to this guide:

http://tronixlabs.com/news/tutorial-l29 ... d-arduino/

To turn on a motor you need to turn 2 'in' pins High or Low (Backwards or Forwards) and send either a PWM command to the respective 'en' pins (enA or enB).

The L298N has a 7.4v LiPo connected to pin 4 and the Nano is powered using the 5v from the L298N pin 6.

Can anyone help with the corrrect commands in python to be sent over i2c?

Pinouts for the code below are:
Nano - L298N
08 - in1
07 - in2
11 - enA

There are two motor connected to pin 1 and 2 / 13 and 14

I have tried:

Code: Select all

setPin(33, "08", "Output")  #on Arduino with I2C ID #33 set pin 08 to OUTPUT
setPin(33, "07", "Output")  #on Arduino with I2C ID #33 set pin 07 to OUTPUT
setPin(33, "11", "Output")
writePin(33,"08", "Low")   #on Arduino with I2C ID #33 set pin 08 HIGH
writePin(33,"07", "High")   #on Arduino with I2C ID #33 set pin 07 HIGH
writePin(33,"11", "High")   #on Arduino with I2C ID #33 set pin 07 HIGH
# analogWritePin(33, "11", 245): #on Arduino with I2C ID #33 set PWM on pin 11 to 245
And:

Code: Select all

setPin(33, "08", "Output")  #on Arduino with I2C ID #33 set pin 08 to OUTPUT
setPin(33, "07", "Output")  #on Arduino with I2C ID #33 set pin 07 to OUTPUT
setPin(33, "11", "Output")
writePin(33,"08", "Low")   #on Arduino with I2C ID #33 set pin 08 HIGH
writePin(33,"07", "High")   #on Arduino with I2C ID #33 set pin 07 HIGH
# writePin(33,"11", "High")   #on Arduino with I2C ID #33 set pin 07 HIGH
analogWritePin(33, "11", "245"): #on Arduino with I2C ID #33 set PWM on pin 11 to 245


But neither seems to work.
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

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

Re: PWM Motor Control Pi Arduino L298N i2C

Sun Jun 28, 2015 1:27 pm

The Arduino seems to serve no purpose. You could just control the motor driver board from the Pi. Is this part of a bigger plan?

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 572
Joined: Sat Nov 02, 2013 2:06 pm

Re: PWM Motor Control Pi Arduino L298N i2C

Sun Jun 28, 2015 2:41 pm

Humor me :-)

I have been running an after School robotics club and have been building Arduino Nano based collision avoidance robots.
What I am doing now is adding a Rasberry Pi to the mix.
The Raspberry Pi will perform the following:
Webserver for web control of the Robot.
Streaming video from a mounted USB webcam.
Voice recognition / voice controlled.
Motion detection.

I have also read that using a Arduino for the basic motor control functions etc frees up valuable GPIO pins, I could do everything with the raspberry Pi but wanted to explore this route.
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 572
Joined: Sat Nov 02, 2013 2:06 pm

Re: PWM Motor Control Pi Arduino L298N i2C

Sun Jun 28, 2015 3:23 pm

I connected up an LED using a 1K resistor connected to pin 3 on the Nano, the LED blinked very momentarily and very dim. I then tried a 470ohm resister and got the same results.
When I looked at the pin status being returned, instead of High it showed a status of P?

Then I checked the pinouts again for the Nano, looking at this page:

http://eezone.co.uk/blog/arduino/arduin ... ble-2.html

Pin 3 is also PWM so maybe setting the status of this pin to OUTPUT and High makes it PWM only??

I then amended the code and changed the wiring to use non PWM pins for the L298N inputs and a PWM for the L298N en pin.
First I tried using a PWM command with no joy, then I tried just to set the L298N en pin to High and when I ran the code Ta Da, the motor span :-)

So maybe when in i2C mode you can't use PWM pins as standard OUTPUTS to the 'in' pins on the L298N??

I still don't know the correct command for PWM to control the speed???
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

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

Re: PWM Motor Control Pi Arduino L298N i2C

Sun Jun 28, 2015 4:21 pm

If you were using an Arduino with the normal Arduino C++ IDE the command to start PWM on a gpio is analogWrite (see https://www.arduino.cc/en/Reference/AnalogWrite).

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 572
Joined: Sat Nov 02, 2013 2:06 pm

Re: PWM Motor Control Pi Arduino L298N i2C

Sun Jun 28, 2015 5:23 pm

Joan,

When looking at the link you provided I also spotted this line:

You do not need to call pinMode() to set the pin as an output before calling analogWrite()

In my code I was also setting the pin as OUTPUT, this may have been confusing the poor thing?
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

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

Re: PWM Motor Control Pi Arduino L298N i2C

Sun Jun 28, 2015 5:26 pm

JonnyAlpha wrote:Joan,

When looking at the link you provided I also spotted this line:

You do not need to call pinMode() to set the pin as an output before calling analogWrite()

In my code I was also setting the pin as OUTPUT, this may have been confusing the poor thing?
That would seem to be unlikely to be the problem. I think they just mean setting it as an output is optional. When you use analogWrite it's obvious it needs to be set as an output.

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 572
Joined: Sat Nov 02, 2013 2:06 pm

Re: PWM Motor Control Pi Arduino L298N i2C

Sun Jun 28, 2015 8:57 pm

I am now experiencing a weird problem, for connection to pin 7 and 12 on the L298N, the enable pins, the only PWM pin that seems to work when sending a high is pin 03 on the Nano?
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 572
Joined: Sat Nov 02, 2013 2:06 pm

Re: PWM Motor Control Pi Arduino L298N i2C

Mon Jun 29, 2015 7:19 am

Investigating this further I have now solved the Pin Status Errors. The pin number for this command needs to be a whole number I.e. if the Pin is no 5 then the command must indicate pin 5 not pin 05.

I am now getting the status back correctly.

For some reason only one of the PWM pins (no 3) works on the enA or enB connections on the L298N. This is the only pin that returns a status of H or L. The remainder all return a status of P.
I guess P is PWM but I still can't get any of these pins to initiate the correct signal to switch on the motors?
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

Return to “General programming discussion”