Connecting Arduino Uno and Electric Imp via Software Serial

I finally got my Arduino Uno talk to one of my Electric Imps. I do not know why, but it was a struggle. But in the end, it is pretty simple. What I would like to archive (in the end) is, that my robot can send the various sensor data to a database, and that I can send commands via web interface. But this is still a long way to got. In this how-to I will focus on the question: How do I connect my Arduino Uno with an Electric Imp using the build in SoftwareSerial library of the Arduino.

Bill of material

What do you need:

  • 1 Electric imp
  • 1 breakout board for the electric imp
  • 1 Arduino Uno
  • Photocell
  • Resistor (1kOhm)
  • Breadboard
  • Wires
  • Power supply for the Arduino (USB or DC)

Layout

After you registered the Electric Imp (how this is done can be found here) you are ready to go.

Via software serial you can use Arduino pins for serial communication or to quote the original text from the SoftwareSerial library:

“The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino, using software to replicate the functionality (hence the name "SoftwareSerial"). It is possible to have multiple software serial ports with speeds up to 115200 bps. “

For my setup I use pin 9 and 10 on the Arduino for the serial communication. These pins have to be connected to the corresponding pins on the electric imp. In my case pin 5 and 7. These pins are also defined in the Electric Imp code.

In addition I use the 3.3V output of the Arduino board to power the electric imp.

Last but not least I added a photocell with a 1kOhm resistor sending sensor data to pin A2 to collect some data to send to the electric imp.

The whole setup is shown in the following picture:
Serial Imp 001 300x300 Connecting Arduino Uno and Electric Imp via Software Serial

Test setup

I put together the whole circuit on a breadboard for testing, so errors can be resolved quickly.

Serial Imp 0063 224x300 Connecting Arduino Uno and Electric Imp via Software Serial

The sparkfun breakout board does not fit well on a breadboard and connecting it, like I did is really sloppy. If you like to have a reliable connection you should solder the wires. But for a quick test it is all right – you have to use your fingers a bit to make sure the wires are connected.

If Arduino and electric imp show some activity you can upload the code to the Arduino and to the Electric Imp.

Arduino code

Serial Imp 004 215x300 Connecting Arduino Uno and Electric Imp via Software Serial

//Adding the library for the software serial
 #include 
 //defining the Pins for TX and RX serial communication
 SoftwareSerial electricimpSerial(9,10);
 //for testing purposes I added a light sensor to gather some data
 const int lightsensor = A2;
 //variable to store the light value
 int lightvalue = 0;
 void setup() {
 // put your setup code here, to run once:
 //open Arduino serial port so values are shown in the Arduino serial monitor
 Serial.begin(9600);
 //open software serial port so values can be passed on to the electric imp
 electricimpSerial.begin(9600);
 // declare the lightsensor as an INPUT for the Arduino
 pinMode(lightsensor, INPUT);
 }
 void loop() {
 // for testing purposes read the value of the lightsensor
 lightvalue = analogRead(lightsensor);
 //Write information to Arduinos serial monitor
 Serial.print("Light level: ");
 Serial.println(lightvalue);
 //Write information to electric imp
 electricimpSerial.print("Ligh level: ");
 electricimpSerial.println(lightvalue);
 // Wait a bit
 delay(2000);
 }

Electric Imp code

The code I use for the Electric Imp is originally written by Jim Lindblom and taken from the sparkfun website. I did some minor adjustments on the code to fit my needs and took the whole part with the LEDs turning on and off out, since I do not need it.

Serial Imp 003 300x165 Connecting Arduino Uno and Electric Imp via Software Serial

// Transmit data between UART and Input/OutputPorts on the impee
// by: Jim Lindblom
//     SparkFun Electronics
// date: September 26, 2012
// license: BeerWare
//          Please use, reuse, and modify this code as you need.
//          We hope it saves you some time, or helps you learn something!
//          If you find it handy, and we meet some day, you can buy me a beer or iced tea in return.
// impeeIn will override the InputPort class. 
// Whenever data is received to the impee, we'll jump into the set(c) function defined within
class impeeIn extends InputPort
{
    name = "UART Out";
    type = "string";

    // This function takes whatever character was sent to the impee
    // and sends it out over the UART5/7. We'll also toggle the txLed
    function set(c)
    {
        hardware.uart57.write(c);
    }
}
local impeeInput = impeeIn();  // assign impeeIn class to the impeeInput
local impeeOutput = OutputPort("UART In", "string");  // set impeeOutput as a string
function initUart()
{
    hardware.configure(UART_57);    // Using UART on pins 5 and 7
    hardware.uart57.configure(9600, 8, PARITY_NONE, 1, NO_CTSRTS); // 19200 baud worked well, no parity, 1 stop bit, 8 data bits
}

// This is our UART polling function. We'll call it once at the beginning of the program,
// then it calls itself every 10us. If there is data in the UART57 buffer, this will read
// as much of it as it can, and send it out of the impee's outputPort.
function pollUart()
{
    imp.wakeup(0.1, pollUart.bindenv(this));    // schedule the next poll in 10us
    local bcnt = 0;
    local s = "";
    local byte = hardware.uart57.read();    // read the UART buffer
    // This will return -1 if there is no data to be read.
    while (byte != -1)  // otherwise, we keep reading until there is no data to be read.
    {
        s+=byte.tochar();
//        s+=byte;
        bcnt++;
        //  server.log(format("%c", byte)); // send the character out to the server log. Optional, great for debugging
        // impeeOutput.set(byte);  // send the valid character out the impee's outputPort
        byte = hardware.uart57.read();  // read from the UART buffer again (not sure if it's a valid character yet)
        // server.show(byte);
    }
    if (bcnt > 0){
//        server.show("String received");
        server.show(s);
        impeeOutput.set(s);
    } 
}
// This is where our program actually starts! Previous stuff was all function and variable declaration.
// This'll configure our impee. It's name is "UartCrossAir", and it has both an input and output to be connected:
imp.configure("UartCommunication", [impeeInput], [impeeOutput]);
initUart(); // Initialize the UART, called just once
pollUart(); // start the UART polling, this function continues to call itself
// From here, the main functions are at play:
//      1. We'll be calling pollUart every 10us. If data is sent from the UART, we'll send out out of the impee.
//      2. If data is sent into the impee, we'll jump into the set function in the InputPort.
//
// The end

Imp setup

When the code is ready, you have to activate the code in the Electric Imp planner and add a “show input” box.

Serial Imp 002 300x85 Connecting Arduino Uno and Electric Imp via Software Serial

If everything is done correctly you should receive the light values. The make sure they are correct open the Arduino serial monitor and you should see the same values.

Serial Imp 005 300x294 Connecting Arduino Uno and Electric Imp via Software Serial

Done!

You may also like...

2 Responses

  1. aklouie says:

    Thanks for the tutorial. It helped answer some of my questions about connecting the Imp to the Arduino. Any chance you could update the section on the Electric Imp Planner to the new Agent IDE?

  2. Elaiel says:

    Hi aklouie
    Thanks for your comment. I have to admit, that I was not on the electric imp page for quite some time as life got in the way ;-). But thanks to you I have a project for the weekend :-).

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

%d bloggers like this: