![how to use l298n motor driver with raspberry pi how to use l298n motor driver with raspberry pi](https://i1.wp.com/www.teachmemicro.com/wp-content/uploads/2018/03/L298N-H-Bridge-Motor-Controller.jpg)
digitalWrite = writePin(device,pinnumber,HIGH/LOW) pinMode = setPin(device, pinnumber, INPUT/OUTPUT/INPUT_PULLUP) Turns the Arduino to a I2C slave device (for the Raspberry Pi)
#HOW TO USE L298N MOTOR DRIVER WITH RASPBERRY PI CODE#
I am also still getting pin status read error:Īnd here is the code I am using to set the Arduino as slave and receive commands from the Pi. I then changed the pins for one motor and used:īy amending the first set of code above (setting pin 3 High and not using PWM) the motor span, trying the second set of code, again amended for the above pin changes and using a PWM command the motor failed to spin. # writePin(33,"11", "High") #on Arduino with I2C ID #33 set pin 07 HIGHĪnalogWritePin(33, "11", "245"): #on Arduino with I2C ID #33 set PWM on pin 11 to 245 # analogWritePin(33, "11", 245): #on Arduino with I2C ID #33 set PWM on pin 11 to 245Īnd here is the same code but this time trying to use a PWM analogueWritePin command: setPin(33, "08", "Output") #on Arduino with I2C ID #33 set pin 08 to OUTPUT
![how to use l298n motor driver with raspberry pi how to use l298n motor driver with raspberry pi](https://electronicsmith.com/wp-content/uploads/2020/03/l298n-woth-arduino.jpg)
WritePin(33,"11", "High") #on Arduino with I2C ID #33 set pin 11 HIGH WritePin(33,"07", "High") #on Arduino with I2C ID #33 set pin 07 HIGH WritePin(33,"08", "Low") #on Arduino with I2C ID #33 set pin 08 LOW SetPin(33, "11", "Output") #on Arduino with I2C ID #33 set pin 11 to OUTPUT SetPin(33, "07", "Output") #on Arduino with I2C ID #33 set pin 07 to OUTPUT There are two motors connected to pin 1 and 2 / 13 and 14 setPin(33, "08", "Output") #on Arduino with I2C ID #33 set pin 08 to OUTPUT Here was my code not using PWM using pins that had worked fine on the avoidance bot: I still don't know the correct command for PWM to control the speed? So maybe when in i2C mode you can't use PWM pins as standard OUTPUTS to the 'in' pins on the L298N?
![how to use l298n motor driver with raspberry pi how to use l298n motor driver with raspberry pi](https://i.stack.imgur.com/vY7I6.png)
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.įirst 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 :-) Pin 3 is also PWM so maybe setting the status of this pin to OUTPUT and High makes it PWM only? Then I checked the pinouts again for the Nano, looking at this page: When I looked at the pin status being returned, instead of High it showed a status of P? I then tried a 470ohm resister and got the same results. When I connected up an LED using a 1K resistor connected to pin 3 on the Nano, the LED blinked very momentarily and very dim. What I am having trouble with is using PWM commands and reading pin status (I keep getting a key error). I have also managed to (finally) spin the motor connected to the L298N again using a Python command on the Rapsberry Pi.Īt first I was having no joy but found that this was because I had been using some of the the Nano PWM as OUTPUTS, when using Sketch without the Pi via i2C on the original bot this was not a problem but when using i2c it seems it may be?
![how to use l298n motor driver with raspberry pi how to use l298n motor driver with raspberry pi](https://i.stack.imgur.com/6SwZk.jpg)
I have successfully interfaced the Arduino as Slave using i2C and have managed to blink the onboard LED using python on the raspberry Pi. I am now trying to add a raspberry Pi connected via i2C to control the motors through the Nano, run a Webcam and stream video to a web page that will also be used to control the robot. I have successfully built a collision avoidance robot using an Arduino Nano AtMega328, HC-SR04 Sensors, 9gMicro Servo (for scanning) and 2 x DC Motors connected via an L298N Motor Driver.