Output Devices

Navigation
  1. Arduino IDE
  2. Downloadfiles

Arduino IDE

I am using a graphic display for my final project.
For that to work with the Arduino IDE I used this library: u8glib_arduino_v1.18.1.zip.
And added it with the arduino library manager (sketch > library > .zip library).

The display used a lot of connections. 3 GNDs, 2 VCCs and 4 PINS smiley


The codes are commented to help you understand.

This code displays a counter:

                    
    #include "U8glib.h"

    U8GLIB_ST7920_128X64 u8g(A3, A2, A1, A0);
    
    // My counter that will be displayed
    int count = 0;
    
    void draw(void) {
        // graphic commands to redraw the complete screen should be placed here  
        u8g.setFont(u8g_font_unifont);
        //u8g.setFont(u8g_font_osb21);
    
        // Position of the text: x , y
        u8g.drawStr( 0, 22, "Count");
    
        // Because our counter is an Integer we have to convert the number into characters to be readible by the u8g library
        char text[5];
        sprintf (text, "%d", (int)count);
    
        // Will draw the counter
        u8g.drawStr( 20, 44, text);
    }
    
    void setup(void) {
        // assign default color value
        if ( u8g.getMode() == U8G_MODE_R3G3B2 )
        u8g.setColorIndex(255); // white
        else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
        u8g.setColorIndex(3); // max intensity
        else if ( u8g.getMode() == U8G_MODE_BW )
        u8g.setColorIndex(1); // pixel on
    }
    
    void loop(void) {
        // picture loop
        u8g.firstPage();
        do {
        draw();
        } while( u8g.nextPage() );
        
        // rebuild the picture after some delay
        delay(3000);
        count++;
    }
                    
                
display

I also want to test if I can display images.
I chose this image to display a smiley:

smiley

And uploaded it to Convertio to get the .bmp file.
Afterwards I need to get the code for the library to display my image.
For that I used the LCD Assistant with these settings:

smiley



You have to save the file and take the hex code and paste it in your arduino code.
With this code my smiley is displayed:

                    
    #include "U8glib.h"

    U8GLIB_ST7920_128X64 u8g(A3, A2, A1, A0);
    
    // The smiley image
    const unsigned char logo [] PROGMEM = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x01, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x01, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x01, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xC0, 0x01, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xC0, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xC0, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xC0, 0x01, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x01, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x01, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };
    
    void setup(void) {
        // assign default color value
        if ( u8g.getMode() == U8G_MODE_R3G3B2 )
        u8g.setColorIndex(255); // white
        else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
        u8g.setColorIndex(3); // max intensity
        else if ( u8g.getMode() == U8G_MODE_BW )
        u8g.setColorIndex(1); // pixel on
    }
    
    void loop(void) {
        // picture loop
        u8g.firstPage();
        do {
        u8g.drawBitmapP( 32, 0, 16, 64, logo);
        } while( u8g.nextPage() );
        
        // rebuild the picture after some delay
        delay(3000);
    }

smiley

Afterwards I used the code from the last assignment to show the output from the sensors on the display.

The code:

            
#include "DHT.h"
#include <SoftwareSerial.h>
#include "U8glib.h"

//PINs connected to the display
U8GLIB_ST7920_128X64 u8g(A3, A2, A1, A0);

// Temp sensor
#define DHTPIN PB1   
#define DHTTYPE DHT11  

// LEDs
#define LED_PIN_ONE 11
#define LED_PIN_TWO PB2
#define PHOTOCELL A4


DHT dht(DHTPIN, DHTTYPE);
int photocellReading;
float h = 0;
float t = 0;
int brightness = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("DHT11 started!");
  
  pinMode(PHOTOCELL, INPUT); // SENSOR

  pinMode(LED_PIN_ONE,OUTPUT);
  pinMode(LED_PIN_TWO,OUTPUT);

  digitalWrite(LED_PIN_ONE, HIGH);
  digitalWrite(LED_PIN_TWO, HIGH);
  
      
  dht.begin();


  // DISPLAY
  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 )
    u8g.setColorIndex(255); // white
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
    u8g.setColorIndex(3); // max intensity
  else if ( u8g.getMode() == U8G_MODE_BW )
    u8g.setColorIndex(1); // pixel on
}

void loop() {
  //Activate Resistor
  digitalWrite(PHOTOCELL, HIGH);

  // Value of photocell
  photocellReading = analogRead(PHOTOCELL);

  //Map values from 0 to 100 - 0 is dark
  brightness = map(photocellReading, 0, 1023, 100, 0);
  
  Serial.print("Analog Photocell: ");
  Serial.println(brightness);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h = dht.readHumidity();
  t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Print to serial monitor
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.println(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C \n");

  // picture loop
  u8g.firstPage();
  do {
    draw();
  } while( u8g.nextPage() );
  
  // rebuild the picture after some delay
  delay(3000);
}

void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_6x12);
  //u8g.setFont(u8g_font_osb21);

  // Position of the text: x , y
  u8g.drawStr( 12, 12, "Temperature:");

  // Cast numbers to text
  char temp[5];
  sprintf (temp, "%d", (int)t);

  // Positioning of the numbers to be right aligned
  if(t >= 10 || t <= (-10)) {
    u8g.drawStr( 90, 12, temp);
  } else {
    u8g.drawStr( 96, 12, temp);
  }
  u8g.drawStr( 108, 12, "C");
  
  u8g.drawStr( 12, 24, "Humidity:");
  char hum[5];
  sprintf (hum, "%d", (int)h);
  if(h >= 10) {
    u8g.drawStr( 90, 24, hum);
  } else {
    u8g.drawStr( 96, 24, hum);
  }
  u8g.drawStr( 108, 24, "%");
  
  u8g.drawStr( 12, 36, "Brightness:");
  char dark[5];
  sprintf (dark, "%d", (int)brightness);
  
  //check how long brightness is
  if(brightness >= 10 && brightness < 100) {
    u8g.drawStr( 90, 36, dark);
  } else if (brightness < 10){
    u8g.drawStr( 96, 36, dark);
  } else {
    u8g.drawStr( 84, 36, dark);
  }
  u8g.drawStr( 108, 36, "%");
  
  u8g.drawStr( 12, 56, "Mode:");
}
            
        

Here you can see the output.
"Temparature" should be "Temperature".
The mode will respresent the mode the user chose to be displayed by the neopixels. For example temperature.

smiley



Downloadfiles

  1. u8glib_arduino_v1.18.1.zip
  2. LCDAssistant.zip