Bug of WiringPi in a Python library for the Pololu DRV8835 Dual Motor Driver Kit for Raspberry Pi

I want to control two little motors with the Pololu DRV8835 Dual Motor Driver Kit for Raspberry Pi.

I found a python library on Github.com and I downloaded it, but when I try to run the example program on the Raspberry, there is an error message, which is about the function wiringpi.wiringPiSetupGpio() on the line 14 of the program pololu_drv8835_rpi.py.

I am a begginner with Raspberry Pi, but I made some researches and I think it is because of the different versions of wiringpi. I think that I have to change the line 1 “import wiringpi” to “import wiringpi2 as wiringpi”, but I am not sure at all.

Can you help me please ?

Thank you !

Hello.

It sounds like you might be using the older, deprecated WiringPi2-Python instead of the library we recommend, WiringPi-Python. Did you follow the instructions in the drv8835-motor-driver-rpi README file, which explain how to install WiringPi-Python? If you followed those instructions and are still getting an error when you run the unmodified library code, please post the exact command you ran and the full error message you are getting.

–David

Thank you very much for you answer !

I remember that I have downloaded the good library but in case of I re-downloaded it one more time but it does not change, there is the same error message.
My command is (in the file ~/drv8835-motor-driver-rpi $) : sudo python example.py

The error message :

Traceback (most recent call last) : 
     File "example.py", line 38, in <module>
          motors.setSpeeds(0, 0)
     File "/home/pi/drv8835-motor-driver-rpi/pololu_drv8835-rpi.py", line 56, in setSpeeds
          self.motor1.setSpeed(m1_speed)
     File "/home/pi/drv8835-motor-driver-rpi/pololu_drv8835-rpi.py", line 44, in setSpeed
          io_init()
      File "/home/pi/drv8835-motor-driver-rpi/pololu_drv8835-rpi.py", line 14, in io_init
          wiringpi.wiringPiSetupGpio()
AttributeError : 'module' object has no attribute 'wiringPiSetupGpio'

Thank you !

I’m not sure why the wiringPiSetupGpio method would not be defined. Could you run the following command and post its output so we can see what version of wiringpi you have?

pip list | grep wiring

And please run this command so we can be sure that the problem is with wiringpi and not caused by our DRV8835 library:

python <<< "import wiringpi; wiringpi.wiringPiSetupGpio()"

On a Raspberry Pi here, that command displays a message about needing to be root, which is good because it means the method was found and executed.

Also, please run this command so we can see what file is actually being imported:

python <<< "import wiringpi; print(wiringpi.__file__)"

–David

Update: Added parentheses to the last command so it can work as-is in Python 3.

Thank you for your help !
Here are the different ouputs :

First command :

pip list | grep wiring

there is a error message :

Usage: pip COMMAND [OPTIONS]
pip: error: No command by the name pip list
(maybe you meant "pip install list")

Should I run an other command to make it work ?

Second command :

python <<< "import wiringpi; wiringpi.wiringPiSetupGpio()"

there is a error message too :

Traceback (most recent call last) : 
File "<stdin>", line 1, in 
AttributeError : 'module' object has no attribute 'wiringPiSetupGpio'

Third command :

python <<< "import wiringpi; print wiringpi.__file__"
/usr/local/lib/python2.7/dist-packages/wiringpi.pyc

Thank you !

Quentin

The output of the third command looks correct; it matches what is happening on my Raspberry Pi.

The second command is giving you the same error you were getting earlier, which shows the error is not somehow caused by our DRV8835 library.

If pip list is not working for you, you must have an older version of pip. Please run these commands instead so we can see what version of wiringpi you have:

ls -l /usr/local/lib/python2.7/dist-packages/ | grep wiring
head /usr/local/lib/python2.7/dist-packages/wiringpi-*/PKG-INFO

When posting a command you ran or its output, please format it as code so that things like __file__ will be printed correctly instead of showing up as file. You can click the edit button on your last post (which looks like a gray pencil) to see how I edited the formatting.

–David

Thank you for your tip !

This is the output for the first command :
ls -l /usr/local/lib/python2.7/dist-packages/ | grep wiring

drwxr-sr-x 2 root uucp 4096 mars 28 09:28 wiringpi2-2.32.3.egg-info
drwxr-sr-x 2 root uucp 4096 mars 21 10:23 wiringpi-2.32.1.egg-info
-rw-r--r-- 1 root uucp  218 mars 28 09:28 wiringpi2.py
-rw-r--r-- 1 root uucp  406 mars 28 09:28 wiringpi2.pyc
-rw-r--r-- 1 root uucp    0 mars 21 10:22 wiringpi.py
-rw-r--r-- 1 root uucp  127 mars 21 11:10 wiringpi.pyc
-rwxr-xr-x 1 root uucp    0 mars 21 10:23 _wiringpi.so

But there is no output for the second command :
head /usr/local/lib/python2.7/dist-packages/wiringpi-*/PKG-INFO

What does it exactly do ?

–Quentin

Hi quentindrvll.

I have the same problem. You must just uninstall and delete wiringpi. But be careful with deleting.

Uninstall: sudo apt-get purge wiringpi

Delete: you must delete everithing from there - /usr/local/lib

You can check it with this command (you can’t have there things like your previous post)
ls -l /usr/local/lib/python2.7/dist-packages/ | grep wiring

Then you just do this: https://github.com/pololu/drv8835-motor-driver-rpi/blob/master/README.textile
and you will be done. Works for me percectly. Sorry for my english.

2 Likes

Thank you very much !!! It works !

Hi all,

I had the same problem and followed Vladisor suggestion.
Ran the purge job and rm:ed everything with wiringpi in it under /usr/local/lib

When I run command python3 <<< "import wiringpi; print wiringpi.__file__", I get Invalid syntax.
I’m using python3.5, what can be wrong?

However, the worst part is that when I run the example code my Pi shuts down immediately. It doesn’t give any error message. So something must be really wrong? I tried it both on a RPi3 and Pizero, they just go dead when I run the code in IDLE3.

I would really appreciate some help on this one.
Thanks!

Hello.

In Python 3, they changed print to be a normal function, so you have to use parentheses around its arguments. This would be the command to run to find out where your wiringpi library is in Python 3:

python3 <<< "import wiringpi; print(wiringpi.__file__)"

If you are still having trouble getting the library to work and would like help troubleshooting, please tell me what operating system you are using, how you installed all the dependencies, the exact command you are trying to run, and the full output from that command.

–David

Thanks for the quick response David and I really do need some help as I’m at a loss here.

So I started out following the instructions here to install wiringpi and drv8835 library.

Then I ran the example.py from Python 3 IDLE and my Raspberry just went black.
After powering up again and running the same code I got the same error as quentindrvll, ending with
AttributeError : ‘module’ object has no attribute ‘wiringPiSetupGpio’

Following the suggested solution by Vladisor and uninstalling and deleting wiringpi and reinstalling wiringpi according to the instructions, my Pis just shuts down after running the example.py.

Here are the outputs from commands:

pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.9.59-v7+ #1047 SMP Sun Oct 29 12:19:23 GMT 2017 armv7l GNU/Linux

pi@raspberrypi:~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian

pi@raspberrypi:~ $ pip3 list | grep wiring
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
wiringpi (2.44.4)

pi@raspberrypi:~ $ python3 <<< "import wiringpi; wiringpi.wiringPiSetupGpio()"

pi@raspberrypi:~ $ python3 <<< "import wiringpi; print(wiringpi.__file__)"
/usr/local/lib/python3.5/dist-packages/wiringpi.py

pi@raspberrypi:~ $ ls -l /usr/local/lib/python3.5/dist-packages/ | grep wiring 
drwxr-sr-x 2 root staff   4096 feb 14 19:04 wiringpi-2.44.4.dist-info
-rwxr-xr-x 1 root staff 689052 feb 14 19:04 _wiringpi.cpython-35m-arm-linux-gnueabihf.so
-rw-r--r-- 1 root staff  24041 feb 14 19:04 wiringpi.py

pi@raspberrypi:~ $ sudo apt-get install python3-dev python3-pip
python3-dev is already the newest version (3.5.3-1).
python3-pip is already the newest version (9.0.1-2+rpt1).

pi@raspberrypi:~ $ sudo pip3 install wiringpi
Requirement already satisfied: wiringpi in /usr/local/lib/python3.5/dist-packages

pi@raspberrypi:~/drv8835-motor-driver-rpi $ sudo python3 setup.py install
running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/local/lib/python3.5/dist-packages/pololu_drv8835_rpi-2.0.0.egg-info
Writing /usr/local/lib/python3.5/dist-packages/pololu_drv8835_rpi-2.0.0.egg-info

It sounds like when you try to run our example code, the Raspberry Pi just shuts down immediately, and this is happening on two different Raspberry Pi models. If you have not done so already, could you remove the motor driver shield and try running the code again to see if the problem still happens? How are you powering the Raspberry Pi? Could you post some pictures of your setup so we can see what is connected to the Raspberry Pi? This will help narrow down the source of the problem.

–David

That’s correct David and I’ve never had any problems like this before.
Running a stepper motor on top of the drv8835 (using stacking header) works fine, just like powering motors with L298N driver with my own code, and I’ve used the Pis for quite a few other projects and nothing like this ever happened.
The PIs are powered from the Official Raspberry Pi Power Supply 5v 2A and right now there is nothing else connected, just running our code kills the Pis.

The PiZero just goes dead, lights out and the Pi3 shuts down all connections and starts to blink with the green led every 2 seconds, you can see it in the picture.

Really strange…

What is the exact command you ran that caused the Raspberry Pi to shut down? If you have not done so already, could you try running that command with sudo added to the beginning and let me know what happens?

–David

Ok, so the problem was sudo. I opened a terminal and typed: disk python3 example.py. This ran the file without breaking the Pi and the motor.
I then connected the Pi using HDMI to see the breakdown better. Opened IDLE (GUI for pyhton 3) and ran the same code. The screen becomes grey, icons change and it says (top right screen) that the SD cars was removed (SL16), so it doesn’t really fall apart nicely… This is what it looks like:

I saw another post on this without a solution here.

So good that it works with sudo from CLI, however it would be nice to have drv8835 work with standard bug free library such as gpiozero etc from IDLE.

Thanks David and looking forward to your thoughts on this one.

/Martin

Hello, Martin.

It seems like there is either a bug in WiringPi or its Python wrapper, or maybe the kernel. Thank you for letting us know. We recommend running WiringPi code with sudo for now. Our library is pretty simple and short, so it probably wouldn’t be too hard to port it over to a different GPIO library.

–David

Thanks David, yes works fine now even with IDLE3, I just added sudo to the command run when clicking the icon, so all good now.
/M