13. Networking and Communications
design and build a wired &/or wireless network connecting at least two processors
I2C Communication
For my final project, I need to make get signal from mpu6050 3-axis accelerometer. But I had no idea to how I2C works, I decided to try with Hello I2C board. I added names on the boards. And I read the wire library reference page on arduino.cc. It was hard to use the wire library because of ATtiny has only few memory for the wire library.
I found a useful website in here. The ATtiny85 (and it's cousins) does not have I2C (or SPI) "built in". Instead it has a Universal Serial Interface (USI) that can be used to facilitate I2C and SPI.
I used Tinywire library, Arduino IDE, SoftwareSerial, but It doesn’t work. I didn’t figure out yet what is the problem.
Download - TinyWire Master library
Download - TinyWire Slave library
Arduino Sketch
I wanted to make simple code that master just pass the data to the slaves which I send it from computer using SoftwareSerial
Master Code on the bridge board
#include <USI_TWI_Master.h> #include <SoftwareSerial.h> SoftwareSerial TinySerial(3, 4); #define Device1_address 0x26 int received; void setup(){ TinyWireM.begin(); TinySerial.begin(9600); TinySerial.println("Tiny Serial Connected"); } void loop() { if(TinySerial.available()) { received = TinySerial.read(); TinySerial.println(" Starting Transmission"); TinySerial.println(received); TinyWireM.beginTransmission(Device1_address); TinyWireM.send(received); TinyWireM.endTransmission(); TinySerial.println("ended"); } } |
Slave Code on the node board
#define I2C_SLAVE_ADDR 0x26 void setup() { pinMode(4, OUTPUT); TinyWireS.begin(I2C_SLAVE_ADDR); } void loop() { byte byteRcvd = 0; if (TinyWireS.available()){ byteRcvd = TinyWireS.receive(); if(byteRcvd ==0x01){ digitalWrite(4, HIGH); }}} |
Failure
Serial Communication works, but the wrong value came out from ATtiny45. I thought that It was problem about 1MHz/8MHz. or because of the ATTiny, there are only mentioned about ATtiny85 on github. I search for the difference with ATtiny45 and ATtiny85, but there are nothing big difference except EEPROM memory. I changed to 1MHz, but It couldn't be uploaded because SoftwareSerial couldn't work on 1MHz. So I tried with no Serial communication and just send specific data to slave on 1MHz. Then It works.
MPU6050 using I2C
I designed for a tiny MPU 6050 sensor and tried to program it using I2C arduino library.
Failure
MPU6050 package is too small to cut by 1/64 mill, so I tried to cut by hand using box cutter. but I trusted myself moo much. as you can see the picture, It was awful to solder MPU6050.
I failed to mill it nice and solder it. so I tried with commercial MPU6050 breakout board (GY521).
Arduino Code :
MPU6050test.ino
Library : Jeff Rowberg has made an I2C lib: http://www.i2cdevlib.com/devices/mpu6050
Reference : http://playground.arduino.cc/Main/MPU-6050
Wire connection : SCL - A5, SDA - A4, INT - D2
Test with Hello Bus
I followed the tutorial with Neil's board and succeed it.