티스토리 뷰

Technology/Arduino

EV3 + Arduino connection via I2C

캡틴테크 2017. 1. 22. 22:23
반응형

I was thinking how to connect between EV3 and Arduino without wifi and bluetooth because Arduino board doesn't have bluetooth module and wifi is working on when All devices are connected to AP. Of course I would like to use wifi module to Arduino in order to communicate but I wish the connection of Arduino and EV3 is possible to use I2C or SPI. Both are general communication way and no need to use wifi or bluetooth at all. 

# Scenario.

What I plan to make solution is, EV3 brick is going to control EV3 medium servo motor to open and close 'Pet Feeder'. Arduino is kind a web server to receive http request and doing action according to request. If Pet Manager sends 'ON' command to Pet Feeder, Pet feeder will be started to pen its door to feed. Once I made Pet Feeder using EV3 block and connected to Medium servo motor. By the way why I try to use Arduino for this project is, main point of this project(making Pet Management solution) is getting knowledge how to use Arduino and how development is easier than other. EV3 is able to cover all Pet Feeder solution but I would like to divide 2 parts which are Feeder Action part and Web server part for Arduino. Actually EV3 is not as much as convenient Arduino. It needs to prepare something or not able to development like Web server which means not flexible to handle. Therefore I manage 2 parts for Pet Feeder.

# Pet Feeder H/W

Like I said, Pet feeder is going to open/close its door and will be constructed by EV3 block and a medium motor. Once I was researching how to make it aspect of mechanical section. Using rotation of servo motor, the door of pet feeder should be worked properly. Here is 1st draft version, It took 3 and half an hour to make it and using basic program in EV3 I could test it how it works.

I expected that more empty space to put food for pet but brick has limited to make it.(I brought it from the office not all !!!). Conceptually, Feeder is working if I send signal, the door of feeder will be open and closed. So far I do not make a code for checking this hardware, Only I used default program in EV3 brick and tested the hardware is good or not.

# Connection between EV3 and Arduino.

This connection between EV3 and Arduino mybe overhead. If I focus to use Arduino, I can use SG-90 servo motor(Already updated article~!!) otherwise EV3 which can control servor motor and connect to wifi. However not sure it would be okay for all and I found out the solution for connection between EV3 and Arduino. Normally, EV3 is connected to motors or sensors via cable which looks lan or telephone not sure what It is exactly. But certain company provides gender to connect EV3 and Arduino using I2C. The gender can be converted the communication.

http://www.mindsensors.com/ev3-and-nxt/58-breadboard-connector-kit-for-nxt-or-ev3

Let's check it what it is, which is small PCB board and possible to connect EV3 and Arduino. I have experience to use I2C based on linux and windows 10 IoT core. It would be easy if it is connected. Once I have ordered this product and got it yesterday and soldered by myself. I referred to the page how to program using Mindstorm.

https://www.dexterindustries.com/howto/connecting-ev3-arduino/

But I could not find the block for Mindstorm. If I make a code for this communication, I should add up addition block module into the program. Finally I found out the similar block to use for I2C, the link is here. It's different hardware but functionality is the same.

https://github.com/DexterInd/EV3_Dexter_Industries_Sensors/blob/master/Dexter.ev3b 

If you know how to control I2C communication, you can handle it with Arduino. EV3 is being 'Master' and Arduino is 'Slave'. Fix the address as '0x04' and the read 1 / 8 bytes or write 1 / 8 bytes. Also provides digitalWrite of arduino function and read analog value. 

Sample code is here : https://github.com/DexterInd/EV3_Dexter_Industries_Sensors/tree/master/EV3_arduino

Arduino I2C library is Wire.h and describe the library detailed in this page(https://www.arduino.cc/en/Reference/Wire)

void setup() 
{
    Serial.begin(9600);         // start serial for output
    Wire.begin(SLAVE_ADDRESS);
    Wire.onReceive(receiveData);
    Wire.onRequest(sendData);
    Serial.println("Ready!");
}

In order to use I2C on Arduino, need to set up some functions. Wire.begin is start function to communicate between master and slave. Wire.onReceiver is register function when Master sends some data to Slave. Wire.onRequest is register function when Master needs to receive data from slave. In my case, Arduino will send the data to EV3 and I register function using Wire.onRequest. EV3 will call every 3 seconds for status which can be made decision whether the door of feeder is opened or not. Here is full code for checking connection between EV3 and Arduino. EV3 is calling to Arduino in order to receive status. If Arduino gets request from EV3, it will be written for value as "0" or "1".

#include 

#define SLAVE_ADDRESS 0x04
byte data;
void setup() 
{
    Serial.begin(9600);         // start serial for output
    Wire.begin(SLAVE_ADDRESS);
    Wire.onReceive(receiveData);
    Wire.onRequest(sendData);
    Serial.println("Ready!");
}

int pin,st,flag=0;

void loop()
{
}

void receiveData(int byteCount)
{
   Serial.println("call receive data");
    while(Wire.available()>0) 
    {
      pin=Wire.read();
      st=Wire.read();
      flag=1;
    }
}

// callback for sending data
void sendData()
{
  Serial.println("call send data");
  Serial.print("flag :");
  Serial.println(flag);
  if(flag == 0){
    flag = 1;
     Wire.write("1");
  }
  else{
    Wire.write("0");
    flag = 0;
  }
}

The brick of LCD will be shown as '1' or '2'

"0" character value is 48 for decimal and "1" character value is 49. Every other second, Brick LCD shows 1 or 2 string.

Now I verified the connection between EV3 and Arduino using I2C and handle it via Mindprogram. I remain work for developing webserver and send data to cloud. The next section, I will explain wifi connection and control using wifi module with Arduino. Thank you to read at the end of this article.


반응형

'Technology > Arduino' 카테고리의 다른 글

[Project] Pet Feeder with Arduino + EV3  (0) 2017.02.12
WIFI + WebServer + Arduino + LED control  (0) 2017.01.31
Actuator control - SG90  (0) 2017.01.15
ISR or Falling??  (0) 2017.01.11
Let's start Arduino Development ~!  (0) 2017.01.02
댓글