Wii Nunchuck Raspberry Pi

Don't want to see this ad? Sign up for anRPF Premium Membershiptoday. Support the community. Stop the ads.

ksj

Well-Known Member
I have been chipping away at things and I just want to get some notes out there. At this point I am tracing down some nasty error codes in what we Linux geeks like to call dependency hell. You need this but you don't have that. You go to install that and this other thing is needed. You go to get this other thing and you need to install this thingamabob. You to go to install the thingamabob and the whachamawhooseit is needed. You go to look for the whachamawhooseit and realise you ran out of coffee, need to sleep and tray again from scratch.
That is dependency hell. I try to take all the guess work out on the write ups I do so others that are less technical do not tear their hair out if they have any left to remove...
https://learn.adafruit.com/16-channel-p ... ng-drivers
https://computers.tutsplus.com/tutorial ... -cms-20984
https://www.raspberrypi.org/forums/view ... 44&t=98318
February 2015 – McPeppr's ... is-broken/
Guide to setting up Wiimote Controllers ... ntrollers/
https://computers.tutsplus.com/tutorial ... -cms-20984
https://raspberrypi.stackexchange.com/q ... g-wiringpi
https://learn.adafruit.com/adafruit-16- ... cking-hats
https://www.raspberrypi.org/forums/view ... p?t=135364
https://raspberrypi.stackexchange.com/q ... wont-start
Wii Nunchuk Gets A Built-in Raspberry Pi Zero ... y-pi-zero/
https://www.raspberrypi.org/forums/view ... p?t=122260

To communicate with the Nunchuk, we must send a handshake signal. If you are using a black Wii Nunchuk, send 2 bytes 0xF0, 0x55 to initialize the first register and 0xFB, 0x00 to initialize the second register of the Nunchuk. On a white Wii Nunchuk, send 0x40, 0x00 followed by 0x00. The I2C address of both Wii Nunchucks is 0x52. The frequency used to communicate with the Wii Nunchuk is`100khz
Source: Guide to setting up Wiimote Controllers ... ntrollers/
OK now the base addresses of the wii nunchuck conflicts with the pca9685 bonnet. 0x40 is shared with the nunchuck. We are not able to change the address of the nunchuck so we have to change the address of the bonnet. In my case I bridged pin a2 to give it a base adress of 0x44. This solved the conflict. I would note that this is a big issue as when you have i2c addresses clash you will get no results when running i2cdetect -y 1. (older versions of the pi may have to use a zero instead of a 1.)
Source: https://learn.adafruit.com/16-channel-p ... ng-drivers
to setup the nunchuck just do these simple steps.
run: sudo raspi-config
under interfacing options select spi, seril, i2c and set them to on. DO NOT turn on 1-wire. (having 1-wire on breaks the pimoroni onoff shim from working (had a hell of a time finding out that one as the pimoroni forums are not exactly like most forums))
in raspi-config is network options and you can connect to wireless from there. If you get errors about wpa_supplicant you will have to run this command:
sudo wpa_suppicant -c "/etc/wpa_supplicant/wpa_supplicant.conf" -iwlan0 -d
If you are using outdated wireless settings you will have to add your network settings to wpa_supplicant.conf manually:
with the following in that file: network={ ssid="networkname" psk="networkpassword" }
Localisation options are where you set the keyboard and other country settings (timezone, wifi country, locale) Linux and keyboards can suck if you do not have the correct mapping and this is where you set it the easy way.
to boot/config.txt add:
dtparam=i2c_arm=on
dtparam=spi=on
dtparam=i2s=on
to /etc/modprobe.d/rspi-blacklist.conf add:
#blacklist spi-bcm2708
#blacklist i2-bcm2708
Now update the pi once you have it on the network:
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo rpi-update
sudo apt-get install python-cwiid
sudo apt-get install wminput
sudo apt-get install bluetooth <-- only needed if you have a very old raspberry pi
sudo apt-get update
sudo apt-get install python-smbus -
sudo pip3 install adafruit-circuitpython-servokit
sudo pip3 install --upgrade setuptools
sudo apt-get install python3-pip
pip3 install RPI.GPIO
pip3 install adafruit-blinka <--failed?
pip3 install adafruit-circuitpython-bme280
sudo pip3 install adafruit-circuitpython-pca9685
sudo pip3 install adafruit-circuitpython-servokit
sudo pip3 install adafruit-circuitpython-busdevice
sudo pip3 install adafruit-circuitpython-register
pip3 install adafruit-circuitpython-lis3dh
sudo pip3 install adafruit-Blinka
sudo pip3 install adafruit-circuitpython-motor
sudo apt-get install python3-smbus


wget https://raw.githubusercontent.com/Boeee ... unchuck.py
sudo python
from nunchuck import nunchuck
wii = nunchuck()
wii.raw()
wii raw will get you directional and accelerometer readings.
source: https://computers.tutsplus.com/tutorial ... -cms-20984

On the Pimoroni onoff shim if you need to not have it mounted just hook up these wires:
5v to 5v, gnd to a gnd. 3v3 to 3v3 for led. io#4 and #17 to the relevant pins.
And make sure 1-wire interface is turned OFF.
From Pimoroni:
We've put together a one-line-installer to install the clean shutdown daemon. It watches the state of BCM pin 17 and, when pulled low (pressed), it initiates a clean shutdown. Last thing, just before your Pi shuts down, BCM pin 4 is pulled low to completely cut power to your Pi.
To install the software, open a terminal and type curl https://get.pimoroni.com/onoffshim | bashto run the one-line-installer.
source: OnOff SHIM – Pimoroni Store
If you need to alter your scripts for startup and shutdown reference this article:
RetroPie Splashscreens - Puzzle Bobble ... pimoroni/4
This is where you can download the servokit library as a standalone
https://github.com/adafruit/Adafruit_Ci ... master.zip
------------------------
wii pca9685 python script
for pca9685:
from adafruit_sevokit import ServoKit
kit = ServoKit(channels=16)
kit.servo[3].angle = 170
from nunchuck import nunchuck
wii = nunchuck()
wii.raw()
/usr/local/lib/python3.5/dist-packages/adafruit_pca9685.py <-- change ox40 to ox44
The wiinunchuck libs can run under python version 1. To run under python version3 you have to update smbus to python3. The pca9685 appears to only work when running under python version 3 or python3 command versus regulary python from the command line.
I will have to rehash the old Arduino code to convert to a usable python script.
At this point I am only focused on getting a working python script.
I will worry about Daemonizing it later.
 

Don't want to see this ad? Sign up for anRPF Premium Membershiptoday. Support the community. Stop the ads.

ksj

Well-Known Member
OK, it is slowly coming together.
This takes the x axis joystick input and translates it into servo movement after taking the reading of 43-258 and remaps to 0-180 degree readings:

from board import SCL, SDA
import busio
from adafruit_pca9685 import PCA9685
from adafruit_motor import servo
i2c = busio.I2C(SCL, SDA)
pca = PCA9685(i2c)
pca.frequency = 50

xjoystick1 = wii.joystick_x(); print(xjoystick1); servo3 = servo.Servo(pca.channels[3], min_pulse=600, max_pulse=2300); servo3.angle = wii.scale(xjoystick1,43,258,0,180);

I will just say that that was fun to figure out as as far as I can find or figure no one has done this combination as of yet. Or if they have it has not been documented and publically shared.

This should allow you to play sounds within a python script:
from pydub import AudioSegment
from pydub.playback import play
song = AudioSegment.from_wav("sound.wav")
play(song)

The values and such are read from this array of commands:
wii.raw() # Returns all the data in raw
wii.joystick() # Returns just the X and Y positions of the joystick
wii.accelerometer() # Returns X, Y and Z positions of the accelerometer
wii.button_c() # Returns True if C button is pressed, False if not
wii.button_z() # Returns True if Z button is pressed, False if not

wii.joystick_x() # Returns just the X position of the joystick
wii.joystick_y() # Returns just the Y position of the joystick
wii.accelerometer_x() # Returns just the X position of the accelerometer
wii.accelerometer_y() # Returns just the Y position of the accelerometer
wii.accelerometer_z() # Returns just the Z position of the accelerometer

wii.scale(value,min,max,out_min,out_max) # Works the same as Arduino Map, perfect for changing values returned to a different scale, ie -100 - +100

GPIO input for a 4 button membrane is next.
 

Don't want to see this ad? Sign up for anRPF Premium Membershiptoday. Support the community. Stop the ads.

Top