Notes About Working with Various Arduino & Netduino Microcontroller Boards

Sunday, October 21, 2012

Using an LCD Display with the Arduino WiFi Shield

The Arduino LiquidCrystal library makes it easy to use an LCD display. (For tutorials and ample code see the Arduino LiquidCrystal page or the Adafruit Learning Systems page). 

The Arduino WiFi shield is an easy but rather pricey (circa $85.00 US) way to connect an Arduino to a wireless network. I find the Arduino WiFi shield easier to use than the LinkSprite Cuhead WiFi Shield V2.0 (but the LinkSprite does work and costs about $30.00 less).

I have created a very simple application with an Arduino Leonardo that uses a TMP36 temperature sensor and displays the temperature on a Web page (using Web server code taken from the WiFiWebServer example contained in the Arduino WiFi library.) The LCD displays the IP address used by the shield and the last temperature reading. My wireless network is encrypted using WPA.

The only real challenge is getting the digital pin connections for the LCD right, since digital pins 4, 7, 10, 11, 12, and 13 are used by the WiFi shield. The code below assumes the following connections between the Arduino and the display. Only differences from the connections in the LCD tutorials above are noted here.

  • LCD4    -->  D8
  • LCD8    -->  D9
  • LCD11   -->  D6
  • LCD12   -->  D5
  • LCD13   -->  D3
  • LCD14   -->  D2
The data line for the TMP36 is connected to the Arduino's Analog 0 pin.

Here's the sketch code:

#include <LiquidCrystal.h>
#include <WiFi.h>
#include <WiFiServer.h>
#include <SPI.h>


char ssid[] = "NETWORL_NAME_HERE"; 
char pass[] = "WPA_PASSWORD_HERE";
int keyIndex = 0;     

int status = WL_IDLE_STATUS;
WiFiServer server(80);
LiquidCrystal lcd(8, 9, 6, 5, 3, 2);

void setup() {
  lcd.begin(16,2);  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) { 
    // don't continue:
    while(true);
  } 
  
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    status = WiFi.begin(ssid, pass);
    // wait 5 seconds for connection:
    delay(5000);
  } 
  server.begin();
  lcd.setCursor(0, 0);
  IPAddress ip = WiFi.localIP();
  lcd.print(ip);
}

void loop() {
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've received a newline character & the line
        // is blank, http request has ended, so you can send 
        // a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // add a meta refresh tag, so the browser pulls again
          // every 60 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"60\">");
          float temp = getTempF();
          client.println(temp);
          lcd.setCursor(0, 1);
          lcd.print(temp);
          client.println("</html>");
           break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(3);
    // close the connection:
    client.stop();
  }
}

float getTempF() {
  float t = (analogRead(0) * 0.004882814 - 0.5) * 100; 
  return t * 9 / 5 + 32;
}

3 comments:

  1. Hi Brad
    i plug a wifi shield on a Arduino Uno R3 and connect a lcd 20x4 Hitachi HD44780 compatible with this scheme: arduino 8, 9, 6, 5, 3, 2 -- lcd RS, E, D4, D5, D6, D7
    but it don't function

    ReplyDelete
    Replies
    1. Hi Pachito -

      Are you using the Arduino WiFi shield or a different one? I assume the LCD works when directly connected to the Arduino, right?

      Delete
  2. Sorry i wrong some connections, it work well

    ReplyDelete