Arduino sample code with SparkFun shield

I’m testing out the sample UART code (http://electricimp.com/docs/api/hardware/uart/configure/) with my SparkFun Arduino shield, but I’m not seeing any serial communications on the Arduino side. The imp and Arduino seem to be working fine otherwise, as I can blink their LEDs manually.

Should this code work with the imp shield straight out of the box? I assume that the shield connects uart57 with the digital pins 0 and 1 on the Arduino without any additional wiring.

Unfortunately the sample code on the SparkFun site (https://www.sparkfun.com/tutorials/397) has yet to be updated to the new IDE.

`// electric imp device code
server.log(“Device Started”);

arduino <- hardware.uart57;
//Port 5=TX , Port 7=RX
//remember: Arduino TX = Imp RX

function arduinoData() {
local b = arduino.read();
while(b != -1) {
local state = “Unknown”;
if (b == 0x10) state = “Off”;
if (b == 0x11) state = "On"
server.log("LED: " + state);
b = arduino.read();
toggleRxLED();
}
}

arduino.configure(57600, 8, PARITY_NONE, 1, NO_CTSRTS, arduinoData);
//****************
local rxLEDToggle = 1; //Yellow LED receiving pin8
local txLEDToggle = 1; //Red LED trnsmitting pin9
function initLEDs(){
// LEDs are on pins 8 and 9 on the imp Shield
// They’re both active low, so writing the pin a 1 will turn the LED off
hardware.pin8.configure(DIGITAL_OUT_OD_PULLUP);
hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
hardware.pin8.write(1);
hardware.pin9.write(1);
}
function toggleTxLED(){
// This function turns an LED on/off quickly on pin 9.
// It first turns the LED on, then calls itself again in 50ms to turn the LED off
txLEDToggle = txLEDToggle?0:1; // toggle the txLEDtoggle variable
if (!txLEDToggle)
{
imp.wakeup(0.01, toggleTxLED.bindenv(this)); // if we’re turning the LED on, set a timer to call this function again (to turn the LED off)
}
hardware.pin9.write(txLEDToggle); // TX LED is on pin 8 (active-low)
}
function toggleRxLED(){
// This function turns an LED on/off quickly on pin 8.
// It first turns the LED on, then calls itself again in 50ms to turn the LED off
rxLEDToggle = rxLEDToggle?0:1; // toggle the rxLEDtoggle variable
if (!rxLEDToggle)
{
imp.wakeup(0.01, toggleRxLED.bindenv(this)); // if we’re turning the LED on, set a timer to call this function again (to turn the LED off)
}
hardware.pin8.write(rxLEDToggle); // RX LED is on pin 8 (active-low)
}
initLEDs();
//****************

function blink(state) {
server.log("Setting LED to: " + state);
arduino.write(state);
toggleTxLED();
imp.wakeup(1.0, function() { blink(1-state); });
}
blink(1);`

Arduino sketch:
`// arduino device code
int led = 13; // led pin number

void setup() {
Serial.begin(9600); // configure serial
pinMode(led, OUTPUT); // configure LED pin
digitalWrite(led, 0); // turn LED off
}

void loop() {
int b = 0;
// if there’s data available
if (Serial.available () > 0) {
// read a byte
b = Serial.read();
if (b == 0x00) {
digitalWrite(led, LOW);
Serial.write(0x10);
} else if (b == 0x01) {
digitalWrite(led, HIGH);
Serial.write(0x11);
}
}
}`

On that Sparkfun shield, the board is defaulted to software serial. You have to jumper it to pins 1 and 0 if you want to use hardware serial. Furthermore if you want pins 8 and 9 then you have to cut a trace on the shield.

I have used this shield and it is what I did.

@mjkuwp94

You are right about the shield is configured to use software serial, but on the shield the serial are connected to pins 8 and 9 according to the schematics (http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Dev/Arduino/Shields/electric-imp-shield-v11.pdf) so no cutting traces are needed and the sample code from Sparkfun should be able to run.

Thanks for the help. Is there an example of using software serial on both ends? I’ve been digging around but a lot of the code out there is deprecated.

The SparkFun page implies that there are jumpers on the underside of the shield for using hardware serial, but there aren’t – do I simply wire pins 5 & 7 to arduino 0 & 1 to enable it?

Sorry for the noob questions, I haven’t done serial comm before.

The impshield has imp pin 5 connected to arduino pin 8 and imp pin 7 connected to arduino pin 9. You can change that by cutting SJ1 and SJ2 and solder them as shown in the schematic (bottom side of the shield).
The software serial library is already included in the Arduino IDE package, just make a reference to it

I set up the most trivial code I could to write data from the imp to the Arduino via software serial, but it’s not receiving anything:

electric imp device code
`
server.log(“Device Started”);

arduino <- hardware.uart57;
//Port 5=TX , Port 7=RX
//remember: Arduino TX = Imp RX

arduino.configure(57600, 8, PARITY_NONE, 1, NO_CTSRTS);

function writeToArduino(toWrite)
{
server.log("sending to Arduino: " + toWrite);
arduino.write(toWrite);
imp.wakeup(1.0, function() { writeToArduino(toWrite); });
}

writeToArduino(“hello?”);`

arduino code
`
#include <SoftwareSerial.h>

SoftwareSerial impSerial(8,9);

void setup()
{
Serial.begin(57600);
impSerial.begin(57600);
}

void loop()
{
if(impSerial.available() > 0)
{
Serial.write(impSerial.read());
}
}`

i can’t try it out now, but start with sending bytes instead of strings

No luck, unfortunately.

Ah ha!

I decided to test out the imp shield by lighting some LEDs on various pins. It turns out that pins 8-12 on the Arduino wouldn’t light the LED. However, if I put a proto shield in between the imp shield and the Arduino, the LEDs would light. I suspect it is because the USB port on the Arduino causes the imp shield to improperly fit into the Arduino.

My trivial sample now works, and I’ll try out your examples.

Thanks!