NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » Interactive LCD (laptop <=> MC device)

April 14, 2009
by ueharaa
ueharaa's Avatar

Hello all, I just wanted to share my first Nerdkits project. It's a small project that I hope will be useful as a starting place for command line interface for future projects. This is my first project, so I offer my experience without any guarantees of anything working :)

Test: To communicate with my device. I want to open a program/command prompt and type something and have the LCD display it.


software requirements:

  1. python
  2. pyserial ** there was some simple python syntax error that needed to be fixed in (I can't remember what it was but, I think it wouldn't run unless you fixed the error. Missing colon perhaps?)
  3. pywin32 ** needed for windows. Linux or mac systems probably won't need this.

wiring requirements: I followed the nerdkits setup for the temperature sensor and , make sure you can copy(burn?) your program to your device.

First Test: first test your connection to your device by running (you can also use putty in serial mode)

python -p YOURPORT -b 115200

if everything is hooked up properly you should see your temperature being displayed to console.

Second Test: I wrote this code to interact using It takes user input and displays it on the LCD and echos the input back to the console. Typing "q" will send it to it's dying loop.


#include <stdio.h>
#include <math.h>
#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"
#include <ctype.h>
#include <avr/pgmspace.h>

static void delay_1s(void) {
  uint8_t i;

  for (i = 0; i < 100; i++)

int main() {
  // start up the LCD
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);

  // start up the serial port
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

// variables to store input
  uint8_t i;
  char buf[25];

//remapping stderr
  stdout = stdin = &uart_stream;
  stderr = &lcd_stream;

//to serial port
  printf_P(PSTR("Hello world! \n"));
//to lcd
  lcd_write_string(PSTR("Good Morning world!"));

  while(1) {
      printf_P(PSTR("Enter command: "));
      if (fgets(buf, sizeof buf - 1, stdin) == NULL)
// q to quit
      if (tolower(buf[0]) == 'q')

//to serial port
      printf_P(PSTR("input: %s\n"),buf);

//to lcd
      fprintf_P(&lcd_stream, PSTR("%s"), buf);

//attempt to get rid of two black spaces on LCD at the end of any string
      for (i=0;i<25;i++){
        if (buf[i]=='/0')
  lcd_write_string(PSTR("I'm dying!"));

  for (i = 0; i < 3; i++)
      putc('.', stderr);
  fprintf(stderr, "\n ");
  lcd_write_string(PSTR("help me! sea otters! "));

  return 0;

Other notes: For some reason I get two black spaces on the LCD when I send text to it, not sure what that is. Perhaps someone on this list can help?

Hope this helps others interested in having their computer talk to their MC and vice versa.


April 14, 2009
by ueharaa
ueharaa's Avatar

sorry about the awful spacing. Not sure what happened, When I originally posted, it did not look that way. -andy

PS. Nerdkits, Can someone fix the spacing on the code part, it looks really awful now :(

April 14, 2009
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Just fixed the spacing -- just use a tab or at least four spaces before any code segments and it will do the right thing in the future.

As for your two black spaces, my guess is that you are getting a "\r\n" (Windows newline) in there. The fgets(...) function will include its terminating newline, but if your terminal sends a two-character \r\n newline, they'll both get included. I see you tried writing some code to get around this, but maybe you can put something like this right before your fprintf_P(...) statement:

for (i=0;i<25;i++){
  if (buf[i]=='\0') {
  } else if(buf[i]=='\r' || buf[i]=='\n') {

The code block above will go through the buffer and if it finds either newline character, it will replace that with a '\0' to terminate the string there.

Also note that I changed your '/0' to a '\0' -- the latter is the correct way to denote a null character, although I think this might be because the forum has a problem with backslashes at the moment!

Let me know if that works!


April 14, 2009
by ranger
ranger's Avatar

somewhat related... is there a simple way to read the information sent over the serial cable from the tempsensor project? Ideally something from a (linux) terminal.

April 14, 2009
by wayward
wayward's Avatar

Assuming you have the serial-to-USB converter and pl2303 driver, then simply reading and writing to and from the special file /dev/ttyUSBx (ttyUSB0 unless you have other serial devices via USB, for example a Palm Pilot via cable) suffices. E.g.:

tail -f /dev/ttyUSB0

will spit out the lines from the MCU to your terminal. Accordingly,

cat > /dev/ttyUSB0

and you can send stuff to the MCU. Use your UNIX-fu for the rest.


May 26, 2009
by tom8787
tom8787's Avatar

Great project, was creating something similar. Tnx for posting!

May 27, 2009
by tom8787
tom8787's Avatar

@ranger: Read this topic:

Post a Reply

Please log in to post a reply.

Did you know that you can connect digital calipers to a microcontroller? Learn more...