I’m, like, totally serial!

I hooked up an old Matrix Orbital LK205-24-USB LCD display with my Arduino today. This 80-character backlit display can be plugged into a computer using the handy dandy USB cable (hence the -USB part of the name) but it turns out it has a 5V TTL serial jumper on the back which can provide power the display, as well as an alternative means to control it.

Arduino + Matrix Orbital LCD display

It was incredibly quick and easy to get it connected up to my new favourite toy, the Arduino. The one thing that took me a few moments to realise (as in, “why isn’t this working? Oh…”) is that the Receive and Transmit pins are relative rather than absolute. It sounds stupid to say it now, but the Rx pin on the Arduino has to be connected to the Tx pin on the LCD display, and vice versa.

Arduino + Matrix Orbital LCD display

It was also a chance to learn how to use the excellent Arduino SoftwareSerial library, which lets you use digital pins as virtual serial connections. Especially handy if you want to have multiple serial connections at one (for example, if you want to continue to use the built in serial port for sending and receiving data from the computer). It even seems to work at 19200 baud required for this display, which I wasn’t expecting. The same thing will work nicely for the Current Cost device (which spits out 3.3V TTL serial at 2400 or 9600 baud depending on the model) too.

Here’s a little Arduino sketch I cobbled together, based on the Software Serial example and this Serial LCD tutorial.

#include <SoftwareSerial.h>
#define rxPin 6 // software Rx pin (connect to Tx on LCD)
#define txPin 7 // software Tx pin (connect to Rx on LCD)

// set up a new serial port
SoftwareSerial swSerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  swSerial.begin(19200);
  // and set the date rate for the real serial port
  Serial.begin(9600);
  delay(100); // (can't use port immediately?)
  clearLCD();
  swSerial.print("Hello, world!");
}

void loop() {
   // retransmit bytes read from the computer to LCD
   if (Serial.available() > 0) {
      byte inchar = Serial.read();
      swSerial.print(inchar);
   }
}

// Clear the LCD (works with my Matrix Orbital LK204-24-USB)
// See http://www.arduino.cc/playground/Learning/SerialLCD
// (and compare with your LCD display docs)
// for many more LCD display helper functions.
void clearLCD(){
  swSerial.print(254, BYTE);
  swSerial.print(88, BYTE);
}

I’m thinking about turning it into an in-house Twitter display, or possibly something which lets me check the artist and title of the current iTunes track being played on our Mac-Mini-acting-as-a-living-room-media-center without having to turn on the television. That’s something I find myself doing at least, ooh, a couple of times per month, so of course a massively overengineered solution technical would be perfect. I have a feeling that as soon as I start using it, I’ll want to use it for more things.

Doorbell update

Quick update to last week’s post about the doorbell project. I’ve now squeezed in into an Altoids tin (surely everybody’s favourite project box), complete with a little hole for the radio antenna. It’s now permanently connected to the Mac Mini in the living room, meaning roo_house on Twitter now makes an update (“There’s somebody at the door”) when the bell is pushed.

Doorbell/Arduino/Altoids

Doorbell/Arduino/Altoids

Hacking the doorbell

I bought a new doorbell. It actually came as a set, with two ringers. One is battery operated and the other is mains powered, plugging straight into the wall. I once again find myself attempting to keep up with Nick. Having seen his doorbell project I knew exactly what I had to do: it was time to hook my doorbell up to an Arduino board and put it on the internet.

Repurposed

The red wire is +3V, the black is ground. The doorbell chime unit used to draw its power from two AA batteries, so now it gets the same three volts from the Arduino instead.

The short dangling white wire is actually the antenna for wireless reception of the signal from the remote button, while the long white wire once completed the circuit to the buzzer. The Arduino treats it as an analog signal (and uses the built in pullup resistors to ‘steer’ the input to high). I should probably use it to drive a transistor to close a digital switch instead. This way works for now though.

The USB cable currently supplies the power and also acts as a serial line, down which the message that the doorbell has been triggered is sent. Eventually I’d like to use an ethernet shield, or even an ethernet-enabled Arduino like this one (which I do hope will be available soon!).

Here’s the Arduino sketch (tweaked slightly from Nick’s blog post).

int ledPin = 13;   // LED connected to digital pin 13
int potPin = 0;    // white doorbell wire to analog pin 0
int val = 0;

long time = 0;
long debounce = 5000;

void setup() {
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  Serial.begin(9600);           // open serial port at 9600 baud
  digitalWrite(14 + potPin, HIGH); // set pullup on the analog pin
                                // (analog 0 = digital 14, a1 = d15, etc)
}

void loop() {
  val = analogRead(potPin);
  if (val < 100) {              // if the circuit is completed
  // (for me, it generally drops from 1023 to ~ 15 when 'ringing')
    if (millis()-time > debounce) {
      Serial.println("ON");
      digitalWrite(ledPin, HIGH);   // sets the LED on
      delay(500);                   // ...wait for half a second
      digitalWrite(ledPin, LOW);    // and turns the LED off
      time = millis();
    }
  }
}

To hook it up to my house’s twitter feed, I just need to open the serial line on the attached computer, doing something like this every time a new line is added

curl -u email@example.com:password -d status="There's somebody at the door" http://twitter.com/statuses/update.xml

Update: it’s now housed in an Altoids tin.

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
The postings on this site are my own and don't necessarily represent my employer's positions, strategies or opinions.