Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Arduino pro mini i2c slave

Tue Jul 26, 2016 2:21 pm

Hello,
i'm starting to play with a arduino pro mini (clone from china) i bought some time ago.
Since it has digital pins, pwm control. analogue in and so on, i thought i would have found tons of sketches to turn this into a "sensor" for raspberry on i2c (something like a mcp23017 + a 8 ways adc and so on)
But my googling miserably failed so i'm wondering if i lost something (About feasibility of this task)
something like that a sketch managing i2c slave and all of this would never fit in arduino's memory.
On the other hand, this could be a stupid idea for a reason i'm missing :)

thanks

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Arduino pro mini i2c slave

Tue Jul 26, 2016 2:50 pm

Of course you can make the Atmel devices i2c slaves; whether there is a library to do this, I do not know-- nor do I care.

I am interested in making the arduino (or pyboard) into an i2S, SPI, or i2c 'slave'. Its very feasible , and will take far less memory than you suspect.

You will need to setup the registers, and you'll need to setup the protocol; which of course means that you will need to read-up on the i2c protocol and try to understand how to get your device to (not only communicate with your protocol) communicate on the i2c bus 'just lilke' any other i2c slave device on the bus !

i2c is tricky; at the low level where understanding occurs; don't get frustrated, dive in and learn. Its absolutely feasible.

marcus
marcus
:ugeek:

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Arduino pro mini i2c slave

Tue Jul 26, 2016 2:55 pm

it's much easier than that..
http://dsscircuits.com/articles/arduino-i2c-slave-guide
this seems to contain every info to create a "working" i2c slave, i'm surprised i could not find an available "pre made" sketch to make a sensor out of an arduino.. that's all :)

User avatar
joan
Posts: 14080
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Arduino pro mini i2c slave

Tue Jul 26, 2016 2:58 pm

There should be an example sketch to handle I2C.

Here's one which I have used on a TI Launchpad using Energia (a sort of clone of the Arduino IDE targeted at TI chips).

Code: Select all

// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin(41);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Wire.onRequest(requestEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

void requestEvent()
{
  Wire.write("hello "); // respond with message of 6 bytes
                       // as expected by master
}

User avatar
mwilliams03
Posts: 121
Joined: Mon Oct 29, 2012 2:56 pm
Contact: Website

Re: Arduino pro mini i2c slave

Wed Jul 27, 2016 6:53 am

Here is some good code for both Pi and Arduino, short and easy.
https://github.com/MikeOchtman/Pi_Arduino_I2C

Beaware that there is a well known i2c bug on the Raspberry Pi to do with clock stretching.
https://github.com/raspberrypi/linux/is ... -217047171
http://www.advamation.com/knowhow/raspb ... c-bug.html

If you are planning on doing many reads a second, you will get random errors. Slowing down the i2c bus can help mitigate this.
Mark -- OzzMaker.com -- Creator of;
+ BerryIMU
+ Berry GPS-IMU
+BerryGPS-GSM
+ PiScreen - A 3.5" TFT for the Rasspberry Pi

Return to “Interfacing (DSI, CSI, I2C, etc.)”