Wii Nunchuk at 3.3V Arduino ARM core

hi,
I am despairing, I can't get my Nunchucks (either single one, I have 2) working with my ARM Cortex M4.

i2c is fine, other devices work well at 100kHz and/or 400kHz, pullups are 4.7k each, either Nunchuck is detected at 0x52 by an i2c scanner.

Nonetheless, I only get untrustworthy jerking values like
joy: -127, -128 acc:-512, -512, -512 but:1, 1
joy: -127, -128 acc:-512, -510, 144 but:1, 1
joy: -127, -128 acc:-512, -512, -512 but:1, 1
joy: -127, -125 acc:-512, -512, -512 but:1, 1
joy: -127, -128 acc:-512, -512, -512 but:1, 1
joy: -127, -128 acc:-509, 511, 511 but:0, 0
joy: 128, 127 acc:511, 511, 511 but:0, 0
joy: 128, -3 acc:4, -1, -470 but:1, 1
joy: 54, 49 acc:-273, 395, -395 but:0, 1
joy: 101, -97 acc:-9, -345, -7 but:0, 1
joy: 2, -1 acc:-472, 195, 214 but:0, 1

or constantly
joyX=255 joyY=255 accX=258 accY=258 accZ=258 btn1=1 btn2=1
joyX=255 joyY=255 accX=258 accY=258 accZ=258 btn1=1 btn2=1
joyX=255 joyY=255 accX=258 accY=258 accZ=258 btn1=1 btn2=1
joyX=255 joyY=255 accX=258 accY=258 accZ=258 btn1=1 btn2=1

either what I'm moving or pressing or not.

I tried the libs e.g.
Wii Nunchuk Controller am Arduino – Teil 1 – Grundlagen – Makerblog.at GitHub - timtro/wiinunchuck-h: A Library for Using the Wii Nunchuk In Arduino Sketches
Fritzing/demo.ino at master · infusion/Fritzing · GitHub
https://www.hackster.io/infusion/using-a-wii-nunchuk-with-arduino-597254
and even some more, in vain.

Also this patch does NOT change anything:

Who knows for certain a working lib or test program which reliably works with ARM core Arduinos?

code example with the patch from

#include <Wire.h>;


void setup(){
   Serial.begin(115200);

   //nunchuck_setpowerpins();
   nunchuck_init();

   Serial.print("Nunchuck ready\n");

}

 

void loop(){
   nunchuck_get_data();
   nunchuck_print_data();
   delay(1000);
}

//=========================================================================//
//Do not modify!!!!!!!!
//=========================================================================//

//
// Nunchuck functions
//


static uint8_t nunchuck_buf[6]; // array to store nunchuck data,

// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins() {
  /*
   //old code, PC2/3 not existing on ARMs; voltage via 3.3V pin
   #define pwrpin PC3
   #define gndpin PC2
   DDRC |= _BV(pwrpin) | _BV(gndpin);
   PORTC &=~ _BV(gndpin);
   PORTC |= _BV(pwrpin);
   */
   delay(100); // wait for things to stabilize
}


// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it

void nunchuck_init()

{  /* 
   // old code
   Wire.begin(); // join i2c bus as master
   Wire.beginTransmission(0x52); // transmit to device 0x52
   Wire.write(0x40); // sends memory address
   Wire.write(0x00); // sends sent a zero.
   Wire.endTransmission(); // stop transmitting
   
  */
   byte cnt;
   uint8_t ctrlr_type[6];

   Wire.begin();
           
// init controller
delay(1);
Wire.beginTransmission(0x52);      // device address
Wire.write(0xF0);                    // 1st initialisation register
Wire.write(0x55);                    // 1st initialisation value
Wire.endTransmission();
delay(1);
Wire.beginTransmission(0x52);
Wire.write(0xFB);                    // 2nd initialisation register
Wire.write(0x00);                    // 2nd initialisation value
Wire.endTransmission();
delay(1);
           
// read the extension type from the register block        
Wire.beginTransmission(0x52);
Wire.write(0xFA);                    // extension type register
Wire.endTransmission();
Wire.beginTransmission(0x52);
Wire.requestFrom(0x52, 6);               // request data from controller
for (cnt = 0; cnt < 6; cnt++) {
   if (Wire.available()) {
       ctrlr_type[cnt] = Wire.read(); // Should be 0x0000 A420 0101 for Classic Controller, 0x0000 A420 0000 for nunchuck
   }
}
Wire.endTransmission();
delay(1);
           
// send the crypto key (zeros), in 3 blocks of 6, 6 & 4.
Wire.beginTransmission(0x52);
Wire.write(0xF0);                    // crypto key command register
Wire.write(0xAA);                    // sends crypto enable notice
Wire.endTransmission();
delay(1);
Wire.beginTransmission(0x52);
Wire.write(0x40);                    // crypto key data address
for (cnt = 0; cnt < 6; cnt++) {
   Wire.write(0x00);                    // sends 1st key block (zeros)
}
Wire.endTransmission();
Wire.beginTransmission(0x52);
Wire.write(0x40);                    // sends memory address
for (cnt = 6; cnt < 12; cnt++) {
   Wire.write(0x00);                    // sends 2nd key block (zeros)
}
Wire.endTransmission();
Wire.beginTransmission(0x52);
Wire.write(0x40);                    // sends memory address
for (cnt = 12; cnt < 16; cnt++) {
   Wire.write(0x00);                    // sends 3rd key block (zeros)
}
Wire.endTransmission();
delay(1);
// end device init
}


// Send a request for data to the nunchuck
// was "send_zero()"
void nunchuck_send_request() {
   Wire.beginTransmission(0x52); // transmit to device 0x52
   Wire.write(0x00); // sends one byte
   Wire.endTransmission(); // stop transmitting
}


// Receive data back from the nunchuck,
// returns 1 on successful read. returns 0 on failure
int nunchuck_get_data() {

   int cnt=0;

   Wire.requestFrom (0x52, 6); // request data from nunchuck   
   while (Wire.available ()) {   
   // receive byte as an integer   
   nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());   
   cnt++;      
}

nunchuck_send_request(); // send request for next data payload
// If we recieved the 6 bytes, then go print them
   if (cnt >= 5) {
     return 1; // success
   }
   return 0; //failure
}


// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits. That is why I
// multiply them by 2 * 2

void nunchuck_print_data() {
   static int i=0;
   int joy_x_axis = nunchuck_buf[0];
   int joy_y_axis = nunchuck_buf[1];
   int accel_x_axis = nunchuck_buf[2]; // * 2 * 2;
   int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
   int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;
   
   int z_button = 0;
   int c_button = 0;
         
   // byte nunchuck_buf[5] contains bits for z and c buttons
   // it also contains the least significant bits for the accelerometer data
   // so we have to check each bit of byte outbuf[5]
   if ((nunchuck_buf[5] >> 0) & 1)
   z_button = 1;
   
   if ((nunchuck_buf[5] >> 1) & 1)
   c_button = 1;
   
   if ((nunchuck_buf[5] >> 2) & 1)
   accel_x_axis += 2;
   
   if ((nunchuck_buf[5] >> 3) & 1)
   accel_x_axis += 1;
   
   if ((nunchuck_buf[5] >> 4) & 1)
   accel_y_axis += 2;
   
   if ((nunchuck_buf[5] >> 5) & 1)
   accel_y_axis += 1;

   if ((nunchuck_buf[5] >> 6) & 1)
   accel_z_axis += 2;

   if ((nunchuck_buf[5] >> 7) & 1)
   accel_z_axis += 1;

   Serial.print(i,DEC);
   Serial.print("\t");
   Serial.print("joy:");
   Serial.print(joy_x_axis,DEC);
   Serial.print(",");
   Serial.print(joy_y_axis, DEC);
   Serial.print(" \t");
   Serial.print("acc:");
   Serial.print(accel_x_axis, DEC);
   Serial.print(",");
   Serial.print(accel_y_axis, DEC);
   Serial.print(",");
   Serial.print(accel_z_axis, DEC);
   Serial.print("\t");
   Serial.print("but:");
   Serial.print(z_button, DEC);
   Serial.print(",");
   Serial.print(c_button, DEC);
   Serial.print("\r\n"); // newline
   i++;

}


// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers

char nunchuk_decode_byte (char x) {
   x = (x ^ 0x17) + 0x17;
   return x;
}


// returns zbutton state: 1=pressed, 0=notpressed
int nunchuck_zbutton() {
   return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1; // voodoo
}


// returns zbutton state: 1=pressed, 0=notpressed
int nunchuck_cbutton()  {
   return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1; // voodoo
}


// returns value of x-axis joystick

int nunchuck_joyx()  {
   return nunchuck_buf[0];
}


// returns value of y-axis joystick
int nunchuck_joyy()  {
   return nunchuck_buf[1];
}


// returns value of x-axis accelerometer
int nunchuck_accelx()  {
   return nunchuck_buf[2]; // FIXME: this leaves out 2-bits of the data
}


// returns value of y-axis accelerometer
int nunchuck_accely()  {
   return nunchuck_buf[3]; // FIXME: this leaves out 2-bits of the data
}


// returns value of z-axis accelerometer
int nunchuck_accelz()  {
   return nunchuck_buf[4]; // FIXME: this leaves out 2-bits of the data
}

output
2 joy:255,255 acc:258,258,258 but:1,1
3 joy:255,255 acc:258,258,258 but:1,1
4 joy:255,255 acc:258,258,258 but:1,1
5 joy:255,255 acc:258,258,258 but:1,1
6 joy:255,255 acc:258,258,258 but:1,1
7 joy:255,255 acc:258,258,258 but:1,1
8 joy:255,255 acc:258,258,258 but:1,1
9 joy:255,255 acc:258,258,258 but:1,1
10 joy:255,255 acc:258,258,258 but:1,1
11 joy:255,255 acc:258,258,258 but:1,1
12 joy:255,255 acc:258,258,258 but:1,1
13 joy:255,255 acc:258,258,258 but:1,1
14 joy:255,255 acc:258,258,258 but:1,1
15 joy:255,255 acc:258,258,258 but:1,1

intermediately, without any action applied:

113 joy:255,255 acc:255,255,170 but:0,0
114 joy:0,0 acc:0,0,0 but:0,0
115 joy:0,0 acc:0,1,164 but:0,0
116 joy:0,0 acc:0,0,0 but:0,0
117 joy:0,3 acc:0,0,0 but:0,0
118 joy:0,0 acc:0,0,0 but:0,0
119 joy:0,0 acc:3,258,258 but:1,1
120 joy:255,255 acc:258,258,258 but:1,1

PS,
I’m using such an adapter:
https://www.ebay.de/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=232700702990&_sacat=0

anyone who got a different tested and working nunchuk code for Arduinos?

update:

this code does not work with my M3 or M4 boards, but with a MEGA2560 it’s fine -
but I need a solution for my ARM Cortex boards!

/*
 *  Nunchuk auslesen
 *
 *  funktioniert mit original Nunchuck
 *  UND mit Wirelesse Nunchuck
 *
 *  Pinbelegung:
 *      3.3V  (Kabel rot)
 *      GND   (Kabel weiß)
 *      A4    (Kabel grün)
 *      A5    (Kabel gelb)
 */

#include <Wire.h>

const int dataLength = 6;             //  numer ob bytes to request
static byte rawData[dataLength];      //  array to store nunchuck data

//  construct to create an enumerated list of the sonsor values returned from the nunchuck
enum nunchuckItems  {  joyX, joyY, accelX, accelY, accelZ, btnZ, btnC };

void setup()
{
    Serial.begin(57600);
    delay(500);
    Serial.println("Labels,Xjoy,Yjoy,Xaccel,Yaccel,Zaccel,Z-btn,C-btn,");
    delay(500);
   
    nunchuckInit();
}

void loop()
{
  if (nunchuckRead() == true)
 {
    Serial.print("Data,");          // Data-Header
    Serial.print(getValue(joyX), DEC);
    Serial.write(",");
    Serial.print(getValue(joyY), DEC);
    Serial.write(",");
    Serial.print(getValue(accelX), DEC);
    Serial.write(",");
    Serial.print(getValue(accelY), DEC);
    Serial.write(",");
    Serial.print(getValue(accelZ), DEC);
    Serial.write(",");
    Serial.print(getValue(btnZ), DEC);
    Serial.write(",");
    Serial.print(getValue(btnC), DEC);
    // Serial.write(",0");
    // Serial.write("\n");
    Serial.println();
 }   
    delay(20);     // time between redraws
}

void nunchuckInit() 
{
    Wire.begin();                  //  join i2c bus as master
    Wire.beginTransmission(0x52);  //  transmit to device 0x52
    Wire.write((byte)0xF0);        //  send memory adress
    Wire.write((byte)0x55);        //  send a zero
    if (Wire.endTransmission() == 0)  {   
         Wire.beginTransmission(0x52);  //  transmit to device 0x52
         Wire.write((byte)0xA5);        //  send memory adress
         Wire.write((byte)0x00);
    }     //  stop transmission
}


//  send a request for data to the nunchuck
static void nunchuckRequest()
{
    Wire.beginTransmission(0x52);    //  transmit to device 0x52
    Wire.write((byte)0x00);          //  send one byte
    Wire.endTransmission();          //  stop transmission
}


//  receive data back from the nunchuck,
//  returns true if read successful, else false
boolean nunchuckRead()
{
    int cnt=0;
    Wire.requestFrom (0x52, dataLength);    //  request data from nunchuck
    while (Wire.available ())  {
        rawData[cnt] = Wire.read();
        cnt++;
    }
    nunchuckRequest();      //  send request for next dat payload
    if (cnt >= dataLength)
        return true;        //  success if all 6 bytes received
    else
        return false;       //  failure
}


//  encode data to format that most wiimote drivers accept
// static char nunchuckDecode (byte x)  {
    // return (x ^ 0x17) + 0x17;
//    return x;
// }


int getValue(int item)   {
    if (item <= accelZ)
        return (int)rawData[item];
    else if (item == btnZ)
        return bitRead(rawData[5], 0) ? 0: 100;
    else if (item == btnC)
        return bitRead(rawData[5], 1) ? 0: 100;
}