Arduino Project: HTML to LCD

Web interactive LCD controlled by Arduino

What you need :

  1. Arduino
  2. An Ethernet Shield and Ethernet Cable
  3. 16×2 Basic Character LCD
  4. A Breadboard or some other means to connect the LCD to the Arduino and
  5. one 2.2 kΩ resistor
  6. some jumper to interconnect the LCD, resistor and Arduino on the breadboard

WHAT IT DOES:

This project combines the above to turn the Arduino into web server which is hooked up to the LCD. It produces a simple HTML web page, from which the user may see what text is currently displayed on the LCD, and provides them the opportunity to change the text using simple input forms. The hardware side of this project is fairly simple, and there were no physical hacks or modifications that needed to be made. The real challenge to this project were working within the limitations of the Arduino as a computing device.

SETUP AND OPERATION:

As we can see, the Ethernet shield is plugged into the Arduino, and is hooked up to the breadboard and LCD according to the scheme outlined in the comments of the source code. This one is completely set up and running, since there is already some text displayed on the LCD. The Arduino’s digital pins 4, 10, 11, 12 and 13 are left free since they are utilized by the shield. All the remaining pins are used by the LCD, so unfortunately this means all of the digital IO pins are used which restricts future additions to this project . The Arduino is also being powered over the USB cable, since this allows me to use the serial monitor to debug, and also because the Wiznet chip and voltage regulator get very hot when my 9V wall adapter is used. I have read that this may be because the shield draws a fair amount of current, and the linear voltage regulator reduces the voltage in accordance with the equation P = I*ΔV. The power is proportional to the current draw, and it is dissipated by the voltage regulator in the form of heat.

With those notes aside, we can connect to the web page by typing in the local network IP address of the Arduino, which I have set up to be fixed using my router’s DHCP reservation functionality. The Arduino did not automatically receive an IP address when the Ethernet cable was first plugged in, so it was necessary to manually add the it to the router’s client list using the mac address on the underside of the shield.

On the web page served by the Arduino it tells us what is currently displayed by the LCD, and it provides us with two boxes to enter more text, one for each row. Each row is limited by the HTML code to 16 characters which provides instant feedback about the limitations of our setup to the user.

Typing in a couple of new lines and clicking the submit button causes the page to refresh and update with what we just entered! In a related issue, due to the RAM limitations of the Arduino and the way symbols are encoded for a URL (a % sign followed by two Hex digits), symbols initially use three times as many bytes as normal alpha-numeric characters. Therefore, if a user were to enter nothing but symbols in each field, the Arduino would crash due to memory issues. To solve this problem I added some code to limit the length of the raw text accepted, but this means that if a user enters too many symbols, the second line will be truncated. In fact, if nothing but symbols are entered into both lines, the second line will be truncated to nothing. It is an unsatisfactory solution, but it’s not an issue if the user uses symbols responsibly.

Putting these issues aside and returning to our example, we can see that the the web page is now displaying what we entered earlier, and that the LCD also reflects the changes made on the web page.

While my Arduino is only hooked up to my local area network, it is possible to release it into the wild world of the Internet, though there is some risk (which I lack the expertise to properly assess) to doing so on your home network. There are some instructions for implementing this athttp://sheepdogguides.com/arduino/art5serv.htm involving changing the settings on your home router and utilizing DynDNS to get a readable and static domain name.

The most difficult aspect of this whole project was without a doubt managing the heavy use of strings within the Arduino’s small amount of RAM. Lots of work went into preventing any major or obvious memory leaks, but I am not entirely sure that I have gotten them all. To ration the memory effectively, I also utilized the ability of the Arduino to store constants in flash memory for the strings of HTML code that are sent to the client’s browser. This means I have at most only a single line of HTML stored in RAM at any time.

That wraps up my first post and first major Arduino project that involved some real coding and which was more than just random experimentation and tinkering. There is still some work that could be done debugging, and I intend to work on some extreme and border conditions to see if there are any bugs remaining that will cause it to crash or do some other bad thing.

(what fun is it if you don’t try to break it?)

As a reminder, the source code for this project can be found Here.



Android Open Accessory Development Kit


Hands-On with the Android Open Accessory Development Kit

a repost from the MAKE Magazine blog

Yesterday, Google announced their open accessory kit based on Arduino. Although they aren’t immediately available for sale in the US, they (or something like what you see in this post) will be soon. Also, the design files are available as part of the ADK, which you can download from here.
NOTE: My understanding is that this is based on the Circuits@Home USB Host Shield. If anyone out there has one of these shields, I’d be really interested in hearing in the comments whether the sample app Just Works for you. You’ll need to wire up the sensors and actuators used in the demo kit, but many of these are simple (buttons, LEDs, relays). WARNING: if you try this, the phone will try to draw a charge from the USB Host Shield, which could cause your Arduino’s voltage regulator to generate a tremendous amount of heat if you’re using an external power supply, and will most likely cause your computer to turn off the USB port if you are using USB. So you can either send 5v into the Arduino, bypassing the voltage regulator, or use a beefy USB power supply (such as the 2 amp USB power supplies that come with the iPad and some phones).
Google gave away a few hundred of the kits here at Google I/O 2011, and I got my hands on one. Inside the box are two sub micro servos, a shield that’s loaded with buttons, LEDs, relays, and more. And the board itself is based on the Arduino Mega design, with the USB host module added (this means that there are two USB ports: one to connect to the Android phone, and the usual one to connect to your computer for programming/serial monitor access):
The complete instructions are here. I’ll cover the highlights below and comment on a couple of the things that confused me when I followed them:
To get started, you’ll need to download the Arduino Software, and also install some add-on libraries from Google and one (CapSense) from the Arduino Playground. Although the Google instructions tell you to install the libraries in the Arduino installation directory, I think you should install them in the libraries directory under the Arduino sketch folder (in Arduino, click Sketch then click Show Sketch Folder, and go up one directory level). Be sure you quit and restart Arduino after you install the libraries.
Next, you need to install the demo sketch to the Arduino-compatible ADK board.
That’s the easy part. Next, you need to go into the Eclipse development environment, and get ready to compile and install the app to your phone. If you have never set yourself up for Android development, set aside about an hour for downloading, installing and configuring and then followthese instructions. After the Android SDK is set up, you will need to install the Google APIs level 10 add-on library and import the sample app, then deploy to your phone. The instructions heretell you how to do it. If you get confused like me, note that the Google APIs level 10 add-on is found under “Third party add-ons.”
And once you get it running, you’ll be able to play around with the demo board. One of the app’s tabs lets you play with inputs (buttons, capacitive sensing, joystick, temperature, light) and the other lets you control outputs (servos, LEDs, etc.). Have fun!

TFT Display with microSD breakout board

128X160 pixels in 18 bit color with a microSD breakout board courtesy of Adafruit Industries

You can purchase the 18-bit color TFT LCD display with microSD card breakout kit (part# ST7735R) from Adafruit Industries


This tutorial from Adafruit is for a 1.8" diagonal TFT display & microSD breakout board. This breakout is the best way to add a small, colorful and bright display to any project. Since the display uses 4-wire SPI to communicate and has its own pixel-addressable frame buffer, it can be used with every kind of microcontroller. Even a very small one with low memory and few pins available!

The 1.8" display has 128x160 color pixels. Unlike the low cost "Nokia 6110" and similar LCD displays, which are CSTN type and thus have poor color and slow refresh, this display is a true TFT! The TFT driver (ST7735R) can display full 18-bit color (262,144 shades!). And the LCD will always come with the same driver chip so there's no worries that your code will not work from one to the other.

The breakout has the TFT soldered on (it uses a delicate flex-circuit connector) as well as a ultra-low-dropout 3.3V regulator and a 3/5V level shifter so you can use it with 3.3V or 5V power and logic. Adafruit board also had a little space to placed a microSD card holder so you can easily load full color bitmaps from a FAT16/FAT32 formatted microSD card.

You can pick up one of these displays in the Adafruit shop!

Flexible wiring

There are two ways to wire up these displays - one is a more flexible method (you can use any pins on the Arduino) and the other is much faster (4-8x faster, but you are required to use the hardware SPI pins) We will begin by showing how to use the more flexible method.

You can use any 4 or 5 pins for this method. We'll show using pins 4, 5, 6, 7, and 8 and once you have it working, you can change those pins around in the wiring and in the sketch.

Start by wiring the power pins.

Connect the leftmost pin to Ground and the next pin to +5V. Connect the rightmost pin (backlight) to 5V as well. If you plug in the Arduino you should see the backlight turn on.

Next connect the RESET (TFT reset pin) and D/C (TFT data/command selection pin).

The RESET pin (3rd from the left) connects to Arduino pin 8. The D/C pin (4th from the left) connectso to pin 7

Finally connect the remaining digital pins, TFT_CS (TFT chip select), MOSI (data sent to TFT) and SCK (clock sent to TFT).

Note that you need to skip a pin on the TFT after D/C - the next wire is from TFT_CS which is 6th from the left. This goes to digital pin6. MOSI (7th from the left) connects to digital pin 5 and finally SCK (8th from the left) connects to digital pin 4

That's it! If you want to change the wiring, you can use any pins but don't forget to change the top of the sketch to match!

//You can use any (4 or) 5 pins #define sclk 4 #define mosi 5

#define cs 6 #define dc 7 #define rst 8

// you can also connect this to the Arduino reset

Test Display

Once you have the display wired up, its time to test your wiring by uploading the example code we have written. Adafruit suggest using an Arduino to test.

Download Adafruit's Arduino library (see bottom of page) from github by clicking on Download in the top right corner. Uncompress the folder and rename it ST7735 - inside the folder you should see the st7735.cpp and st7735.h files. Install the ST7735 library foler by placing it in your arduinosketchfolder/libraries folder. You may have to create the libraries subfolder if this is your first library.You can read more about installing libraries in this tutorial

Restart the Arduino IDE. You should now be able to select File > Examples > ST7735 > graphicstest sketch. Upload the sketch to your Arduino wired as above.

Once uploaded, the Arduino should perform all the test display procedures! If you're not seeing anything - first check if you have the backlight on, if the backlight is not lit something is wrong with the power/backlight wiring. If the backlight is lit but you see nothing on the display make sure you're using our suggested wiring

High Speed SPI-Wiring

If you want to connect to the display and have high-speed data transfer (4-8x faster) you'll have to use the hardware SPI system. This is optimized to be faster than the flexible wiring method (because its built into the hardware of the chip) but you are required to use the hardware SPI pins!

On Atmega 328/168/8 type Arduinos ('classic' type) the hardware SPI pins are 11 (MOSI), 13 (SCK) and 10 (CS). For Megas it is 51(MOSI), 52 (SCK), and 53 (CS). The CS pin can be a different pin but if you use any other pin you must still have the hardware SPI CS pin (10 or 53) as an output!

We will also change the TFT_CS pin to be pin 10 and D/C to be pin 9 (you can change these two later but pin 10 must always be an output for hardware SPI to work)

Wiring Diagram

Select the File > Examples > ST7735 > graphicstest_highspeed sketch. Upload the sketch to your Arduino wired as above.

In the sketch we changed the pin definitions

//#define sclk 13

// for MEGAs use pin 52

//#define mosi 11

// for MEGAs use pin 51 #define cs 10

// for MEGAs you probably want this to be pin 53 #define dc 9 #define rst 8

// you can also connect this to the Arduino reset

To use the hardware SPI, we commented out this line

// Option 1: use any pins but a little slower

// ST7735 tft = ST7735(cs, dc, mosi, sclk, rst);

and uncommented this line:

// Option 2: must use the hardware SPI pins

// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be

// an output. This is much faster - also required if you want

// to use the microSD card (see the image drawing example) ST7735 tft = ST7735(cs, dc, rst);

Now when you run the graphics test you'll notice its much faster.

If you want to usethe microSD card and TFT at the same time, you'll need to use the hardware SPI because the SPI pins are shared between the two. See the bitmaps tutorial below for details on how to do this.

Graphics Library

Adafruit has written a full graphics library specifically for this display which will get you up and running quickly. The code is written in C/C++ for Arduino but is easy to port to any microcontroller by rewritting the low level pin access functions. Here are some of the functions we've included in the library

First thing to note is that color is 16-bit (the driver can handle 18-bit but 16-bit is good enough for most applications and fits neatly into a 2-byte word), and that includes Red, Green and Blue in a 16-bit variable. The way the color is packed in is the top 5 bits are red, the middle 6 bits are green and the bottom 5 bits are blue.

For solid colors, we have this handy cheat-sheet. Of course, you can pick any of 65,536 colors but while starting out, this might be helpful

// Color definitions #define BLACK 0x0000 #define BLUE 0x001F #define RED

0xF800 #define GREEN 0x07E0 #define CYAN 0x07FF #define MAGENTA

0xF81F #define YELLOW 0xFFE0 #define WHITE 0xFFFF


Drawing pixels

First up is the most basic pixel pusher. You can call this with two coordinates and a color and it will make a dot:

 void drawPixel(uint8_t x, uint8_t y, uint16_t color);

You can also draw lines, with a starting and end point and color

void drawLine(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint16_t color);

If your lines are vertical or horizontal, you can call an optimized drawing function that doesn't do all the angular calculations.

void drawVerticalLine(uint8_t x0,uint8_t y0,uint8_t length,uint16_t color);

Next up, rectangles and squares can be drawn and filled using the following procedures. If you want a recangle that has a contrasting outline color, fillRect first, then drawRect over it.

void drawRect(uint8_t x0,uint8_t y0,uint8_t w,uint8_t h,uint16_t color);

Likewise, for circles, you can draw and fill.

void drawCircle(uint8_t x0, uint8_t y0, uint8_t r, uint16_t color);

If you need to add some text, there are two basic string drawing procedures. The first is just for a single character. You can place the character anywhere, and with any color. You only have one font to save on space, and its meant to be 5x8 pixels. If you pass size as 1 or if you don't put down a size, that will be what is used. If you need bigger text, the code can scale the font up by passing in a larger size(integer). It's a little blocky but this keeps the flash usage down so we don't need multiple fonts.

void drawChar(uint8_t x, uint8_t y,char c, uint16_t color,uint8_t size);

These primitives should get you started!

Displaying Bitmaps

In this example, it shows how to display a 128x160 pixel full color bitmap from a microSD card

Adafruit has an example sketch in the library showing how to display full color bitmap images stored on an SD card. You'll need a microSD card such as this one . You'll also need to download Adafruit's SD library modified to allow faster reads (these changes will hopefully be added to arduino v23) but for now you can download the new library here . Download the library by clicking the Downloads button and uncompressing the folder. Replace the files in your ArduinoIDE/libraries/SD folder (make a backup of course) and restart the IDE.

You'll also need an image. Adafruit suggest starting with this bitmap of a parrot. If you want to later use your own image, use an image editing tool and crop your image to no larger than 160 pixels high and 128 pixels wide. Save it as a 24-bit color BMP file - it must be 24-bit color format to work, even if it was originally a 16-bit color image - becaue of the way BMPs are stored and displayed!

Copy the parrot.bmp to the microSD card and insert it into the back of the breakout board

Wire up the TFT according to the high-speed SPI diagram above. Test that your wiring is correct by uploading the graphics test sketch with the high speed SPI line uncommented and the flexible-low-speed wiring commented.

Once you are sure that the TFT is wired correctly, add the two wires for talking to the SD card. Connect CARD_CS (the unconnected pin in the middle) to digital pin 4 (you can change this later to any pin you want). Connect MISO (second from the right) to the Arduino's hardware SPI MISO pin. For Classic Arduinos, this is pin 12. For Mega's this is pin 50. You can't change the MISO pin, its fixed in the chip hardware.

Now load the bitmap example sketch into the Arduino. It should display the parrot image. If you have any problems, check the serial console for any messages such as not being able to initialize the microSD card or not finding the image.


Downloads

You can download Adafruit's Arduino library with examples from github . To install it, rename the downloaded and uncompressed library toST7735 and place in the sketchfolder/libraries folder. See Adafruit's detailed tutorial for more info.

You may also be interested in the datasheet for the display , and display driver chip