sutha
Posts: 2
Joined: Wed Dec 19, 2018 7:25 pm

How to control the direction of wheels using Motor HAT?

Wed Dec 19, 2018 11:13 pm

HI,
I am a beginner to Python and Raspberry pi. I am developing a robot. For that, I have used four DC motors and a motor HAT (bought from the seller U-GEEK, but looks like ADAFRUIT). While running DCTest.py , my motor works. But I dunno how to change the direction of the wheels using that HAT. I know how to control the direction using GPIO pins. But I am not familiar with I2C, SDA and SCL pins. Help me to solve this problem.

scotty101
Posts: 3682
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to control the direction of wheels using Motor HAT?

Thu Dec 20, 2018 12:00 am

Is it the same HAT as in this tutorial?

https://learn.adafruit.com/simple-raspb ... t?view=all

If you are using the same library as they are you can just use a backward command.

If you aren't using the same code as them, perhaps you can share this link to what you are using so we can help.

Have you tried passing a negative number to reverse the direction? Like -1.0?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

rhubarbdog
Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: How to control the direction of wheels using Motor HAT?

Thu Dec 20, 2018 3:59 am

There is 1 method and 2 values your interested in.

Code: Select all

motor.run(Adafruit_MotorHAT.BACKWARD)
motor.setSpeed(10)

Code: Select all

motor.run(Adafruit_MotorHAT.FORWARD)
motor.setSpeed(10)
I have this line at the top of my program

Code: Select all

from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor

sutha
Posts: 2
Joined: Wed Dec 19, 2018 7:25 pm

Re: How to control the direction of wheels using Motor HAT?

Fri Dec 21, 2018 12:21 pm

Hi,
Thanks for your suggestions.

It is not a Adafruit one. I bought from the seller U-GEEK in Amazon.

The codes I used to run the motors are given below:

#!/usr/bin/python
from Raspi_MotorHAT import Raspi_MotorHAT, Raspi_DCMotor
import time
import atexit

mh = Raspi_MotorHAT(addr=0x6f)

def turnOffMotors():
mh.getMotor(1).run(Raspi_MotorHAT.RELEASE)
mh.getMotor(2).run(Raspi_MotorHAT.RELEASE)
mh.getMotor(3).run(Raspi_MotorHAT.RELEASE)
mh.getMotor(4).run(Raspi_MotorHAT.RELEASE)

atexit.register(turnOffMotors)

myMotor = mh.getMotor(3)

# set the speed to start, from 0 (off) to 255 (max speed)
myMotor.setSpeed(150)
myMotor.run(Raspi_MotorHAT.FORWARD);
# turn on motor
myMotor.run(Raspi_MotorHAT.RELEASE);


while (True):
print "Forward! "
myMotor.run(Raspi_MotorHAT.FORWARD)

print "\tSpeed up..."
for i in range(255):
myMotor.setSpeed(i)
time.sleep(0.01)

print "\tSlow down..."
for i in reversed(range(255)):
myMotor.setSpeed(i)
time.sleep(0.01)

print "Backward! "
myMotor.run(Raspi_MotorHAT.BACKWARD)

print "\tSpeed up..."
for i in range(255):
myMotor.setSpeed(i)
time.sleep(0.01)

print "\tSlow down..."
for i in reversed(range(255)):
myMotor.setSpeed(i)
time.sleep(0.01)

print "Release"
myMotor.run(Raspi_MotorHAT.RELEASE)
time.sleep(1.0)

I can execute these codes and run the motors through command prompt window. However, if I am trying to run the program in Python, it says that "import error: No module Raspi_MotorHAT exist.

1) How to execute these codes in Python.

2)Is it possible to control the DC motor HAT using GPIO pins. Suppose if I am giving logic HIGH to board pin 7, one DC motor should run and others should not be run.

3)Through the above program, I can ON and OFF the motors. I could not logically ON and OFF the motors.

4)Suggest me some books or articles to understand I2C and GPIO pins.

rhubarbdog
Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: How to control the direction of wheels using Motor HAT?

Fri Dec 21, 2018 1:42 pm

Where's the file raspi_MotorHAT.py or directoty raspi_MotorHAT containing some python files?
Have you installed it using both pip and pip3?

scotty101
Posts: 3682
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to control the direction of wheels using Motor HAT?

Fri Dec 21, 2018 3:02 pm

If this isn't an Adafruit HAT, why do you expect the Adafruit code to work?

Have you installed the python code recommended by the seller of the HAT?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

rhubarbdog
Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: How to control the direction of wheels using Motor HAT?

Fri Dec 21, 2018 3:51 pm

The OP said it looked like Ada fruit so i thought give it a try.
When you see the code the OP posted they didn't do much to obsficate from ada fruit motor hat code

Return to “Beginners”