Message Boards Message Boards

Reading sensor data from a Wii nunchuck via Arduino to Raspberry Pi

GROUPS:
Using the wiiChuck Adapter, you can easily connect the wii nunchuck to an arduino.

WiiChuck Adapter



The following arduino program, adapted from the wiichuck demo programmed by Tod Kurt, waits for a serial request from the Raspberry Pi and sends back the accelerometers data + two button states.
 /*
  * WiiChuckDemo --
  *
  * 2008 Tod E. Kurt, http://thingm.com/
  *
  */
 
 #include <Wire.h>
 #include <nunchuck_funcs.h>

int inByte=0;
byte accx,accy,accz,zbut,cbut;
int ledPin = 13;

void setup()
{
    Serial.begin(57600);
    nunchuck_setpowerpins();
    nunchuck_init(); // send the initilization handshake
}

void loop()
{
    if( Serial.available()>0 ) {
        inByte = Serial.read();
        nunchuck_get_data();
        accx  = nunchuck_accelx();
        accy  = nunchuck_accely();
        accz  = nunchuck_accelz();
        zbut = nunchuck_zbutton();
        cbut = nunchuck_cbutton();
         
        Serial.write((byte)accx);
        Serial.write((byte)accy);
        Serial.write((byte)accz);
        Serial.write((byte)zbut);
        Serial.write((byte)cbut);
    }
}
In the Mathematica for the Raspberry Pi, wanted to chart the values of the three accelerometers as I record different movements with the nunchuck. (The values of the axis go somewhere between 80-220.
serial = DeviceOpen["Serial", {"/dev/ttyACM0", "BaudRate" -> 57600}];
ping := Module[{x}, DeviceWriteBuffer[serial, {"5"}];

  x = DeviceReadBuffer[serial][[1 ;; 3]]]
data = Transpose@Table[ping, {i, 120}];
ListLinePlot[data, PlotStyle -> {Red, Thick, Dashed},

PlotLegends -> {"X", "Y", "Z"}]
POSTED BY: Diego Zviovich
Answer
4 years ago
Diego, this is great! - Thank you for sharing. If we understood the setup correctly, the so called “WiiChuck” Wii Nunchuck Adapter was used. One can read about it at the following blog: todbot blog. Here are a few images with a greater detail of the adapter.



POSTED BY: Moderation Team
Answer
4 years ago
That's really cool! Thanks for sharing!
POSTED BY: Arnoud Buzing
Answer
4 years ago
Someone mentioned that using a logic level converter and the Raspberry Pi I2C bus could work without Arduino. Do you think it would?
POSTED BY: Sam Carrettie
Answer
4 years ago

Ok.. This is great. But I tried this froma Windows 8.1 computer running Math 10. I replace the Arduino code with the AnalogRead potentiometer sample sketch.

/*
  ReadAnalogVoltage
  Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

I got the following error

serial = DeviceOpen["Serial", {"COM3", "BaudRate" -> 9600}];
ping := Module[{x}, DeviceWriteBuffer[serial, {"5"}];
  x = DeviceReadBuffer[serial][[1; 1]]]
data = Transpose@Table[ping, {i, 120}]

I got a lot of errors..

    During evaluation of In[40]:= Part::partw: Part 1 of {} does not exist. >>

During evaluation of In[40]:= Part::partw: Part 1 of {} does not exist. >>

During evaluation of In[40]:= Part::partw: Part 1 of {} does not exist. >>

During evaluation of In[40]:= General::stop: Further output of Part::partw will be suppressed during this calculation. >>

During evaluation of In[40]:= Transpose::nmtx: The first two levels of {13,{}[[1]],53,{}[[1]],46,{}[[1]],13,{}[[1]],{}[[1]],{}[[1]],{}[[1]],10,{}[[1]],{}[[1]],48,{}[[1]],{}[[1]],53,{}[[1]],{}[[1]],10,{}[[1]],{}[[1]],48,{}[[1]],{}[[1]],46,{}[[1]],{}[[1]],10,{}[[1]],{}[[1]],48,{}[[1]],{}[[1]],48,{}[[1]],{}[[1]],53,{}[[1]],{}[[1]],13,{}[[1]],{}[[1]],{}[[1]],48,{}[[1]],{}[[1]],10,{}[[1]],<<70>>} cannot be transposed. >>

Out[42]= Transpose[{13, {}[[1]], 53, {}[[1]], 46, {}[[1]], 
  13, {}[[1]], {}[[1]], {}[[1]], {}[[1]], 10, {}[[1]], {}[[1]], 
  48, {}[[1]], {}[[1]], 53, {}[[1]], {}[[1]], 10, {}[[1]], {}[[1]], 
  48, {}[[1]], {}[[1]], 46, {}[[1]], {}[[1]], 10, {}[[1]], {}[[1]], 
  48, {}[[1]], {}[[1]], 48, {}[[1]], {}[[1]], 53, {}[[1]], {}[[1]], 
  13, {}[[1]], {}[[1]], {}[[1]], 48, {}[[1]], {}[[1]], 
  10, {}[[1]], {}[[1]], 48, {}[[1]], {}[[1]], 48, {}[[1]], {}[[1]], 
  46, {}[[1]], {}[[1]], 10, {}[[1]], {}[[1]], 48, {}[[1]], {}[[1]], 
  53, {}[[1]], {}[[1]], 53, {}[[1]], {}[[1]], 13, {}[[1]], {}[[1]], 
  48, {}[[1]], {}[[1]], {}[[1]], 10, {}[[1]], {}[[1]], 
  48, {}[[1]], {}[[1]], 48, {}[[1]], {}[[1]], 46, {}[[1]], {}[[1]], 
  10, {}[[1]], {}[[1]], 48, {}[[1]], {}[[1]], 53, {}[[1]], {}[[1]], 
  13, {}[[1]], {}[[1]], {}[[1]], 13, {}[[1]], {}[[1]], 
  46, {}[[1]], {}[[1]], 10, {}[[1]], {}[[1]], 48, {}[[1]], {}[[1]], 
  46, {}[[1]], {}[[1]], 46}]

In[30]:= data

Out[30]= Transpose[{{48, 13, 10}, {}[[1 ;; 3]], {48, 48, 13}, {}[[
   1 ;; 3]], {10, 53, 46}, {}[[1 ;; 3]], {10, 53, 46}, {}[[
   1 ;; 3]], {}[[1 ;; 3]], {48, 48, 13}, {}[[1 ;; 3]], {48, 13, 
   10}, {}[[1 ;; 3]], {}[[1 ;; 3]], {48, 13, 10}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {46, 48, 48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {10, 53, 
   46}, {}[[1 ;; 3]], {}[[1 ;; 3]], {13, 10, 53}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {48, 48, 13}, {}[[1 ;; 3]], {}[[1 ;; 3]], {53, 46, 
   48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {13, 10, 53}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {46, 48, 48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {46, 48, 
   48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {}[[1 ;; 3]], {10, 53, 46}, {}[[
   1 ;; 3]], {}[[1 ;; 3]], {48, 13, 10}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {46, 48, 48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {13, 10, 
   53}, {}[[1 ;; 3]], {}[[1 ;; 3]], {48, 48, 13}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {48, 48, 13}, {}[[1 ;; 3]], {}[[1 ;; 3]], {53, 46, 
   48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {13, 10, 53}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {46, 48, 48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {}[[
   1 ;; 3]], {46, 48, 48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {10, 53, 
   46}, {}[[1 ;; 3]], {}[[1 ;; 3]], {46, 48, 48}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {46, 48, 48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {13, 10, 
   53}, {}[[1 ;; 3]], {}[[1 ;; 3]], {48, 48, 13}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {48, 48, 13}, {}[[1 ;; 3]], {}[[1 ;; 3]], {53, 46, 
   48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {48, 13, 10}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {}[[1 ;; 3]], {46, 48, 48}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {10, 53, 46}, {}[[1 ;; 3]], {}[[1 ;; 3]], {10, 53, 
   46}, {}[[1 ;; 3]], {}[[1 ;; 3]], {48, 13, 10}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {53, 46, 48}, {}[[1 ;; 3]], {}[[1 ;; 3]], {13, 10, 
   53}, {}[[1 ;; 3]], {}[[1 ;; 3]], {48, 48, 13}, {}[[1 ;; 3]], {}[[
   1 ;; 3]], {48, 48, 13}}]
Attachments:
POSTED BY: Jose Calderon
Answer
3 years ago

Group Abstract Group Abstract