1

I have a raspberryPi and Arduino connected via i2C. On the RPi I have a python script that takes int values and simply sends them to the RPi, which in turn echoes them back. I am trying to debug this, since I am unable to send ints larger than 255.

One thought was that I might be able to connect the arduino slave directly to my Mac, and then use the Arduino serial monitor to help with debugging.

Unfortunately the python script will not run, because I assume i2C can only be run on a Linux kernel.

Am I mistaken?

This is an edited portion of my python code

import smbus
import time
# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)

# This is the address we setup in the Arduino Program
address = 0x04

def writeNumber(value):
    bus.write_byte(address, value)
    return -1

def readNumber():
    number = bus.read_byte(address)
    return number
  • Your Mac probably does not have an i2c interface, unless you want to see if you can misapply one built into a legacy monitor connector (which might require kernel hacking that would be a bit harder than on linux). How about using a simple USB logic analyzer? Likely your problem with sending values larger than 255 is a software one - that's the limit for one byte, so you'll need to use multiple bytes to do more. Can you add your code to the question? – Chris Stratton Jul 18 '14 at 15:50
  • Just plug the arduino into the Pi or your mac, so you can also have the serial connection for debugging. But be careful with the power. You should connect the arduino to both 5v on the Pi and 5v via the usb (mac or pi). If you are powering the arduino using the 3.3v of the Pi you might need some voltage level shifting circuitry to protect the Pi's GPIO pins from 5v (they only accept 3.3v) – Gerben Jul 19 '14 at 14:58
1

No, your Mac doesn't have I2C support. It was pointed out in the comments that there might be some legacy port, but it won't be usable due to driver limitations.

You're getting a few things confused. USB is it's own technology, separate from I2C/UART/SPI. I'm not sure how you would even wire I2C to your Mac. The serial monitor in Arduino is for USB COM ports, not related to I2C.

As far as debugging, there's a few options. First of all, there are a few ways to debug I2C using an oscilloscope. If that's too expensive, you could also get away with a logic analyzer. The one I just linked was at Sparkfun for $150. If that's still too expensive and you're willing to risk $30, you can find cheap ones on eBay (most with little/no documenation).

I don't know your code, so I can't advise you on how to fix it. If you need to debug at such a low level, extra hardware is needed.

It doesn't seem like a maximum of 255 that odd, since that's the maximum number that you can do with one byte. You might have to split data up into multiple bytes with some sort of delimiter character. With some quick mental math, it seems as two bytes can give you numbers up to 65,535 if you combine both bytes (i.e. 10101010 + 01010101 = 1010101001010101).

Edit: this seems like the Arduino code you want:

#include <Wire.h>

#define SLAVE_ADDRESS 0x04
int number = 0;
int num1 = 0;
int num2 = 0;
int state = 0;
boolean secondByte = false;

void setup() {
    pinMode(13, OUTPUT);
    Serial.begin(9600);         // start serial for output
    // initialize i2c as slave
    Wire.begin(SLAVE_ADDRESS);

    // define callbacks for i2c communication
    Wire.onReceive(receiveData);
    Wire.onRequest(sendData);

    Serial.println("Ready!");
}

void loop() {
    delay(100);
}

// callback for received data
void receiveData(int byteCount){
    while(Wire.available()) {
        if(secondByte) {
            num2 = Wire.read();
            number = (num1 << 8) + num2;

            Serial.print("data received: ");
            Serial.println(number);

            if (number == 1){
                if (state == 0){
                    digitalWrite(13, HIGH); // set the LED on
                    state = 1;
                }
                else{
                    digitalWrite(13, LOW); // set the LED off
                    state = 0;
                }
             }
         } else {
             num1 = Wire.read();
         }
         secondByte != secondByte;
    }
}

// callback for sending data
void sendData(){
    Wire.write(num1);
    Wire.write(num2);
}

The Python code will have to do the similar thing. Note: the first part of the number (reading left to right) will be sent first.

  • Thanks for the GREAT info. Excuse me , since I am a novice arduino hacker, but would I be better off in using int values, or converting to strings before sending to the arduino via i2C? That really is the first thing I have to get right, have been stumbling for the past few days simply trying to send int values larger than 255. part of my problem is that I cannot 'SEE' what is being recieved by the arduino...feel like a blind man without even being able to see any trace calls. Apologies for the length of this comment.....really trying to get this to work! Feeling only frustration right now :( – Bachalo Jul 18 '14 at 18:36
  • This is my 'starter' code which works for simply echoing int values 0-255. blog.oscarliang.net/raspberry-pi-arduino-connected-i2c/But 2 problems. Can't send values larger than 255 AND can't stream data in realtime. My goal is to send MouseXY coordinates in realtime... – Bachalo Jul 18 '14 at 18:43
  • @eco_bach I might be overlooking something, but I can't find your code that you use to send/receive. Also, I don't really know what you need help with... it might give you more answers if I move it to another SE site. Can you please clarify exactly what you want (i.e. need help with Python, need help with Arduino code, etc.) and I'll try to help you and/or get this question in the right place. Thanks! – Anonymous Penguin Jul 18 '14 at 18:57
  • Thanks. Need help on BOTH Python and Arduino side, sending-receiving int values larger than 255. linking slightly incorrect please see blog.oscarliang.net/raspberry-pi-arduino-connected-i2c – Bachalo Jul 18 '14 at 19:56
  • @eco_bach is the edit what you want? – Anonymous Penguin Jul 18 '14 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.