# Standard boilerplate to call the main() function to begin L.start(runFrequency) # run will be called with this frequency Self.rc = remoteControl.RemoteControl(self.port) # Keep in mind these are not pins but servo outs # Configure min and max servo pulse lengths Self.port = 5555 # Port to open so the remote can connect RunFrequency = 1 # The frequency on wich the main Task will be ran This is the main entry point of the application, and contains the robot class: import RPi.GPIO as GPIOįrom twisted.internet import reactor, taskĮnableMotors = True # Motors can be disabled for testing with this
L298n motor driver raspberry pi zero w code#
I divided the code in these files, that I will be explaining one by one: So I’m pretty happy with it, it’s responsive enough and super intuitive to drive.
On this video you can see how it worked out: To accomplish this I am using my android’s phone accelerometer, with an app that sends to an IP address and port, using UDP protocol, the changes in the accelerometer (and other stuff, that I´m not currently using. The first task I wanted to tackle on this robot was enabling it to be remote controled. I love object oriented programming, and python gave me the perfect combination between OOP an access to hardware. Programming in python was so simple, that I am regretting not making the change long before. It simplified a lot the coding and made it look a lot more tidy, since many cables staid on the lower level instead of needing to connect them to the RPi. To control the motors I used a 16 servo driver, and I think that was the best decision I made on this robot. This ensures I wont get weird stuff when the motors are draining current. I’m not using them right now, but I thought it was a good idea to lay out all the hardware in place, so I can avoid surprises down the path, when I finally want to use them.Īnother choice I made was to use two different batteries, one for electronics, and another for the motors. I bought an Arduino Smart Car kit in Aliexpress, there is a lot of information online about this chassis, and it’s not expensive at all.Īs I mentioned, the brain of the robot is a Raspberry Pi Zero W, as can be seen in the images, I also connected to it the camera module and 3 SR04 ultrasonic distance sensors. I chose to use it as brain of one of those standard robot bases available online.
L298n motor driver raspberry pi zero w how to#
Save this code in Raspberry Pi Pico and name it main.As soon as I got my Raspberry Pi Zero W, I started thinking how to test it.
My code logic is when I press one button, motor will rotate clock wise and when I release that button motor will stop and when I press another button motor will rotate counter clock wise and when I release button motor will stop rotating.Ĭode is given as below from machine import pin To write and upload program here i used Thonny IDE. (adsbygoogle = window.adsbygoogle || ).push() To power motors, I connected 12v DC adaptor. You can use any value of resistor above 1K Ohm. Then Connect GND pin of L298N with GND Pin of Pico, Make comman connection between 12V GND and raspberry pi pico’s GNDĬonnect push button as shown in schematic diagram. Now Connect IN4 pin of L298N with GP3 Pin of Pico Then Connect IN2 pin of L298N with GP1 Pin of PicoĬonnect IN3 pin of L298N with GP2 Pin of Pico 12 Volt Adaptor Schematic Diagram for DC motor control with Raspberry Pi Picoįollow this schematic diagram and make connections.Ĭonnect IN1 pin of L298N with GP0 Pin of Raspberry Pi Pico