interface an Arduino Uno as an i2c slave to EV3?

HomeForumsMonoBrick EV3 Firmwareinterface an Arduino Uno as an i2c slave to EV3?

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #4439
    Author Image
    Helmut Wunder
    Participant

    hey,
    I would urgently need code to interface an Arduino Uno or Duemilanove(==2009 😉 ) as an i2c slave to EV3.
    Please code for both Arduino (Sketch) and EV3 (C#) will be appreciated!
    For the start, a quite simple example will do (write digital pin for LED (if touch sensor pressed at EV3), read touch sensor state on Arduino (e.g. pin dig11), read Arduino analog resistor value (e.g. pin ana3), display all values on EV3 screen).
    Thank you all very much in advance!

    #4443
    Author Image
    Helmut Wunder
    Participant

    ps,
    an Arduino Sketch code for i2c communication is already available, but maybe it has to be changed to work reliably with the corresponding C# code:

    #include <Wire.h>
    
    #define SLAVE_ADDRESS 0x04
    int number = 0;
    int state = 0;
     
    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){ // ... maybe add a delay(15) to go easy on the i2c bus... ???
     
        while(Wire.available()) {
            number = Wire.read();
            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;
                }
             }
         }
    }
     
    // callback for sending data
    void sendData(){
        Wire.write(number);
    }

    so any help for Mono/C# code will be appreciated!
    Thanks in advance!

    #4447
    Author Image
    Helmut Wunder
    Participant

    hey,
    by NXC for NXT I got it working – one just would need to transcode the NXC source into Mono/C# –
    who would like to do it? 😎

    http://www.mindstormsforum.de/viewtopic.php?f=25&t=8302&p=65015#p65015

    #4448
    Author Image
    Tcm0
    Participant

    Does anyone have a working example for any I2C sensor (maybe an official one)? It would be helpfull when trying to write something for the Arduino.

    #4449
    Author Image
    Tcm0
    Participant

    Nevermind, I got it: Klick me.
    Here is my wiring diagram (yes, my breadboard is a mess): Klick me.
    And now the code.
    My Arduino class:

    using System;
    using MonoBrickFirmware;
    using MonoBrickFirmware.Sensors;
    
    namespace EV3Temeplate
    {
        class Arduino : I2CSensor
        {
           public Arduino(SensorPort Port, byte adress)
               : base (Port, adress, I2CMode.LowSpeed)
            {
               base.Initialise();
            }
    
            public byte[] ReadReg(byte Register, byte Length)
            {
                return base.ReadRegister(Register, Length);
            }
    
            public void WriteReg(byte Register, byte[] Data)
            {
                base.WriteRegister(Register, Data);
            }
    
            public byte[] ReadAndWriteReg(byte Register, byte[] Data, int rxLength)
            {
                return base.WriteAndRead(Register, Data, rxLength);
            }
    
            public override string ReadAsString()
            {
                throw new NotImplementedException();
            }
        }
    }

    My program.cs:

    using System;
    using System.Threading;
    using MonoBrickFirmware;
    using MonoBrickFirmware.Display;
    using MonoBrickFirmware.Sensors;
    using MonoBrickFirmware.UserInput;
    
    namespace EV3Temeplate
    {
        class Program
        {
            static void Main(string[] args)
            {
                EventWaitHandle stopped = new ManualResetEvent(false);
                LcdConsole.WriteLine("Connecting...");
                byte Adresse = 0x04 << 1;
                
                ButtonEvents buts = new ButtonEvents();
                Arduino Arduino1 = new Arduino(SensorPort.In1, Adresse);
                byte[] Ergebnis = new byte[1];
                LcdConsole.WriteLine("Connected");
                LcdConsole.WriteLine("Press Enter to read I2C Port");
                LcdConsole.WriteLine("Press Escape to leave program");
                buts.EscapePressed += () => { 
    			    stopped.Set();
    		    };
                buts.EnterPressed += () => {
                    Ergebnis = Arduino1.ReadReg(0x04, 1);
                    LcdConsole.WriteLine("Result: " + Convert.ToString(Ergebnis[0]));
                };
                stopped.WaitOne();
            }
        }
    }

    and the program that runs on the Arduino (sorry, I can’t organize it): Klick me.

    • This reply was modified 10 years, 5 months ago by Author ImageTcm0.
    • This reply was modified 10 years, 5 months ago by Author ImageTcm0.
    #4459
    Author Image
    Helmut Wunder
    Participant

    hey,
    congratulations, this looks like a very good start!
    but will you even try to establich a communication protocol for alternately, rotatory sending and receiving a data array like byte array[14] like I did in my NXC example? That would be great if we finally could access an Arduino by a EV3 similar like as it’s already possible by a NXT !

    http://www.mindstormsforum.de/viewtopic.php?f=25&t=8302#p65015

    #4460
    Author Image
    Tcm0
    Participant

    Why don’t you port your NXT code to EV3? 😛

    #4461
    Author Image
    Helmut Wunder
    Participant

    why do you ask? you should know it, we already discussed this beyond measure –
    For the EV3 there is no complete C-based API available – no sensor- and no i2c library, just a Lego lms2012 header which is bugged like hell.
    I also don’t understand anything about C#, Visual Studio, WiFi, classes, objects, implementing, derivating, uses, extending –
    just know something about straight procedures and functions like by ANSI C or Sketch or Pascal. OOP is simply a mess to me.

    But the NXC code and the Sketch code (both actually simplified procedural C) have been established by me, top-down, within a couple of days (as you know).
    http://www.mindstormsforum.de/viewtopic.php?f=25&t=8302&p=65015#p65015

    If I knew how to do it by C# I wouldn’t have started my thread about Brick/Arduino-i2c-communication.

    But as you are experienced in C# – : if it was that easy, why didn’t you proceed with your beginnings ?
    Where’s the coding problem ?

    #4478
    Author Image
    Helmut Wunder
    Participant

    any ideas for a transcoding?

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

Posted in

Make a donation