Simple Arduino code on the EMFCamp Badge. Part1.

Untitled
One of the high points of this summer for me was going to  EMF Camp. Apart from an amazing experience, I came away with a rather smart electronic badge called a TILDAe.  Back home I’ve had a look to see what I could do with it.

There are detailed instructions on the emfcamp wiki on how to download the code and set up a development environment (it uses Arduino 1.5.7) so you can re-flash the badge or extend the code on it. Having given it a go,  I found I could reload the code that was on the badge perfectly. But, to do anything useful would involve putting my C++ hat on, which, to say the least, is pretty dusty these days. Also, doing anything in the Arduino IDE was going to quickly get pretty painful. So, time to move over to Eclipse and get the books out? Then I remembered  reading about it being  ‘Arduino Due’ compatible:

At its heart the badge is an Arduino Due compatible 32bit ARM Cortex M3. A rechargeable battery will keep it running for days, and you can charge it over USB when the juice runs out. We added a 128×64 pixel LCD screen, two RGB LEDs, a radio transceiverjoystickaccelerometergyroscopespeaker,infrared, and all sorts of other fun parts. It’s compatible with Arduino shields and has dedicated connectors for electronic textiles.

So instead, I started to look at how to set up a straightforward, native, Arduino environment, that would let me talk to the badge, put stuff on the screen, work the radio*, flash leds and get information from the gyro/accelerometer. What I wanted was *simple* and maybe be useful enough to others to get a few more of these things out of drawers.

*If you want to play with the radio, you’ll need one of these £20 SRF/USB sticks from Ciseco or another badge – I’d recommend the stick as they’re dead easy to use.

Setup.

  1. Download the badge code from github https://github.com/emfcamp/Mk2-Firmware and unzip it somewhere for reference later.
  2. Download the Arduino 1.5.7beta code (this has support for the Due). http://arduino.cc/en/main/software
  3. Make yourself a new directory, open the arduino IDE and in preferences change the Sketchbook folder to this directory.
  4. Restart Arduino. [This gives you a clean distinct environment to play in]
  5. Plug in your badge and turn it on.
  6. Tools/Board. Change to Arduino Due (NativeUSB Port)
  7. Tools/Port. Pick your usb port. Mine says /dev/tty/usbmodem1d111(Arduino Due (NativeUSB Port))
  8. You’re ready to go.

A word about ports.

On there Due, there are two USB ports. If you have spent the last few years writing Serial.println(“some debug message”) in your code, you’ll find nothing happens. That’s because the port attached to the IDE is actually called SerialUSB in Arduinospeak. So, if you want debug messages, it’s SerialUSB.println(). Now you might ask,  what does Serial do? Well in this case it’s attached to the SRF radio (so your messages maybe going somewhere after all). I’ll come back to that.

Hardware/wiring.

The badge code if installed as per the wiki instructions contains new board definitions you can then pick in the IDE. One of the side effects of this is that it sets the definitions for where the various Arduino pins point. So, for instance it lets you simply use LED1_RED without knowing what pin it actually is on the badge. In this case I haven’t used it as a) I wanted to know and b) It comes with ‘baggage’, such as the libraries, header files etc – remember the simple environment goal? Not to mention that a lot of the Arduino folk seem to think if you’re doing board definitions, you’re past the point where you should be using Eclipse or similar anyway. The result, though, is that sketches need to be told what thing is attached to what pin, but since it’s all neatly listed in this Header file, that’s not really an issue. If you’re looking for where the joystick is, or what buttons do what, it’s all in there and more.

Note. If you’re interested in the actual hardware, there’s also TiLDA Mk2 Prototype v0.333.pdf, which shows how it’s all put together.

Test one: Blink.

No Arduino writeup would be complete without a Blink sketch. There’s no led on pin 13 on the badge, but there are a couple of tri-colours available and I’ll use one of them instead. Here’s the sketch:

/*
Tilda LED test.
Hacked from Adafruit Arduino – Lesson 3. RGB LED
*/

//Pulled from the Hardware definition
#define LED1_BLUE (37u)
#define LED1_GREEN (39u)
#define LED1_RED (41u)
 
void setup()
{
 pinMode(LED1_RED, OUTPUT);
 pinMode(LED1_GREEN, OUTPUT);
 pinMode(LED1_BLUE, OUTPUT); 
}
 
void loop()
{
 setColor(255, 0, 0); // red
 delay(1000);
 setColor(0, 255, 0); // green
 delay(1000);
 setColor(0, 0, 255); // blue
 delay(1000);
 setColor(255, 255, 0); // yellow
 delay(1000); 
 setColor(80, 0, 80); // purple
 delay(1000);
 setColor(0, 255, 255); // aqua
 delay(1000);
}
void setColor(int red, int green, int blue)
{
 analogWrite(LED1_RED, red);
 analogWrite(LED1_GREEN, green);
 analogWrite(LED1_BLUE, blue); 
}

Upload this to your badge and a single LED should flash. Don’t worry if your screen is now blank. That’s next.

LCD Screen.

It’s no good having code on the badge if you can’t put stuff on the screen. The LCD used on the badge is an ST7565, but I couldn’t get the provided GLCD library to work, which is what I suspected (and no doubt due to lack of C++ skills on my part). Luckily, a scout around on the internet led me to the heroic Kimball Jojnson (@drrk) who had a GLCD library for the badge that didn’t reference the rtos software and which you can get from https://github.com/drrk/glcd-tilda. Once downloaded, Sketch -> Import Library -> Add Library will add it into your /library folder as glcd-tilda. 

You need to make one change. Remember what I said about the board definition? At the moment the library won’t compile since it doesn’t know what pins the LCD connects to. So, in /library/glcd-tilda folder, find glcd_Device.cpp , and right after “#include “SPI.h” on line #31 add the following lines and then save the file.

#define LCD_CS (52u)
#define LCD_POWER (40u)
#define LCD_BACKLIGHT (35u)
#define LCD_A0 (38u)
#define LCD_RESET (34u)

That’s it. Now we can go for the classic “Hello World”.

Test two: Hello World.

Start a new sketch and put in the following. Notice we need the SPI library from the standard Arduino  library as well, since the LCD is a serial device:

#include <SPI.h>
#include <glcd.h>
#include <fonts/allFonts.h>
#define LCD_POWER (40u)
#define LCD_BACKLIGHT (35u)
void setup() {
 //Turn LCD On :-)
 pinMode(LCD_POWER, OUTPUT);
 digitalWrite(LCD_POWER, LOW);
 //Turn Backlight On
 pinMode(LCD_BACKLIGHT, OUTPUT);
 digitalWrite(LCD_BACKLIGHT, HIGH);
 //Init LCD
 GLCD.Init(NON_INVERTED); 
 GLCD.SelectFont(System5x7);
 GLCD.print("Hello, world!");
 GLCD.display();
}
void loop() {
 GLCD.CursorTo(0, 1);
 // print the number of seconds since reset:
 GLCD.print(millis()/1000);
 GLCD.display();
 delay(1000);
}

Notice, that I had to physically turn the LCD on (that foxed me for a while) as well as the backlight if needed. Also,  the LCD code actually writes to a 1k buffer first, so nothing will happen until you call GLCD.display() which writes the buffer to the actual device. If you upload this sketch, the screen should spring into life in classic style.

It’s likely you’ll want to do something more complicated than just ‘Hello’. To find out what’s available, have a look in the docs at http://playground.arduino.cc/Code/GLCDks0108 as this library is modified from glcd-arduino (GLCDv3)  and has the same functions.

This probably isn’t the only library that will work, and with more effort the provided GLCD one might as well. That would be handy, as this library doesn’t have some niceties like screen rotation. I’ve heard that the UG8Lib library works with the Due/ST7565 combo, so that might also be worth a try.

Something useful? Basic Clock.

It’s nice to end on something that’s a little bit useful. The M3 chip at the heart of the board has a handy Real-Time-Clock or rtc so a simple clock is the obvious choice. It uses another library https://github.com/MarkusLange/Arduino-Due-RTC-Library from the pen of Markus Lange, which is in the badge code or you can get it from the link above. Add into your library as you did for the LCD and you’re ready to go. Start a new sketch and put in the following:

#include <rtc_clock.h>
#include <SPI.h>
#include <glcd.h>
#include <fonts/allFonts.h>
#define LCD_POWER (40u)
#define LCD_BACKLIGHT (35u)
// Select the clock source
RTC_clock rtc_clock(RC);
int old_unixtime;
char* daynames[]={"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
 
void setup() {
 SerialUSB.begin(9600);
 //Turn LCD On :-)
 pinMode(LCD_POWER, OUTPUT);
 digitalWrite(LCD_POWER, LOW);
 //Turn Backlight On
 pinMode(LCD_BACKLIGHT, OUTPUT);
 digitalWrite(LCD_BACKLIGHT, HIGH);
 //Init LCD
 GLCD.Init(NON_INVERTED);
 rtc_clock.init();
//This doesn't work as RTC setting don't survive - not sure why
//So, default is last compile Date/Time
 if (rtc_clock.date_already_set() == 0) {
  rtc_clock.set_time(__TIME__);
  rtc_clock.set_date(__DATE__);
 }
//Small fomt for header
 GLCD.SelectFont(System5x7);
 GLCD.print("EMF Camp TILDAe Clock");
//Big font for time
 GLCD.SelectFont(fixednums15x31);
 GLCD.display();
 }

void loop() {
 if ( rtc_clock.unixtime() != old_unixtime) {
 old_unixtime = rtc_clock.unixtime();
 char buffer[10];
 sprintf( buffer, "%02d:%02d:%02d",
 rtc_clock.get_hours(),
 rtc_clock.get_minutes(),
 rtc_clock.get_seconds());
 GLCD.CursorTo(0, 1);
 GLCD.print(buffer);
 GLCD.display();
 }
}

UntitledNote. Even though there’s a battery on the badge it doesn’t remember the RTC settings if you turn it off. I reset it in the code to the last compile date/time as default. More investigation needed.

What next?

  • There’s no way to set the time yet. I’ll leave that to you – there’s buttons aplenty.
  • No date display.

Wrap Up.

The TILDA badges are fine bits of kit. With a few mods, I’ve a nice simple environment on the Arduino IDE, the basics working and the beginnings of a handy clock. Next time I’ll have a look at the accelerometer, gyroscope and the radio. Until then, as always,  it’s all on github…. https://github.com/Tingenek/EMFBadge