Know How...

Mar 30th 2017

Know How... 298

Arduino 102: Part II

Program and attach an OLED i2C to your Arduino project!

Although the show is no longer in production, you can enjoy episodes from the TWiT Archives.
Category: Help & How To

Go through the steps to include an OLED screen, a Real Time Clock, and another LCD screen all attached through i2C. Also the different types of memory an Arduino uses and how to save to EEPROM. 

 

OLED i2C

.96" i2C OLED Display Module    ~$6    
        
Basic Screen Access        
Code Example: _01_OLED        
"#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4

Adafruit_SSD1306 display(OLED_RESET);
"        
"void setup()   
{                
  // Turn on i2C Functionality
  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
}
"        
"void loop() 
{
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print(""Hello World!"");
  display.setCursor(0,20);
  display.print(""Goodbye Cranky Hippo"");
  display.display();
  delay(1000);
  display.clearDisplay();
  display.display();
  delay(1000);
}"        
** Writes static text to the OLED Screen        
Demo Agenda        
1. Libraries (ADAfruid)        
2. Text Size        
3. setCursor        
4. display.display()        
5. display.clearDisplay()        
6. The need for Freeze/Delays        

 

 There are three types of Memory in an Arduino        
    1. Flash (Where we store the sketch)        
    2. SRAM (Static Random Access Memory: System Memory)        
    3. EEPROM (Electrically Erasable Programmable Read Only Memory: OUR space!)        
            
    On our ATmega328 based Arduinos (Both the Uno and the Nano)        
    Flash: 32K bytes        
    SRAM: 2k        
    EEPROM: 1K (1024)        
            
    Simple Counter        
    Code Example: _03_Counter        
    "#include <Wire.h> 
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
#define buttonPin1 2

Adafruit_SSD1306 display(OLED_RESET);

int OnOff;
int counter=0;"        
    "// This is the variable that stores the state of the button"
void setup()
{
  // Setting the mode for the pin to which the button is connected
  pinMode(buttonPin1, INPUT);     
  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
}"        
    "void loop()
{
  // Call the first Button
  OnOff = digitalRead(buttonPin1);
  if (OnOff == HIGH)
  {     
    counter = counter + 1;
    display.clearDisplay();
    display.display();
    display.setTextSize(4);
    display.setTextColor(WHITE);
    display.setCursor(30,3);
    display.print(counter);
    display.display();
  } 
}"        
    ** This program counts up every time the button is pushed        
            
    Demo Agenda        
    1. Wire up the button        
    2. Add the pins and variables         
    3. If Statement        
            
            

EEPROM            
            
 Code Example: _04_EEPROM  

     
    "#include <Wire.h> 
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
#define buttonPin1 2
#define buttonPin2 3

Adafruit_SSD1306 display(OLED_RESET);

int OnOff=0;
int TriggerEEPROM=0;
int counter=0;"        
    "// This is the variable that stores the state of the button"
void setup()
{
  // Setting the mode for the pin to which the button is connected
  pinMode(buttonPin1, INPUT);     
  Wire.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)
  counter = EEPROM.read(100);
}"        
    "void loop()
{
  display.setTextSize(4);
  display.setTextColor(WHITE);
    
  // Call the first Button
  OnOff = digitalRead(buttonPin1);
  if (OnOff == HIGH)
  {     
    counter = counter + 1;
    display.clearDisplay();
    display.display();
    display.setCursor(30,3);
    display.print(counter);
    display.display();
  } 

  TriggerEEPROM = digitalRead(buttonPin2);
  if (TriggerEEPROM == HIGH)
  {
    while(TriggerEEPROM == HIGH)
    {
      TriggerEEPROM = digitalRead(buttonPin2);
    }
    display.clearDisplay();
    display.display();  
    EEPROM.put(100, counter);
  }
}"        
                        
    Demo Agenda        
    1. Wire up three more buttons (+, -, store, read)        
    2. Add a decrement function        
    3. Add a store function        
    4. Add a restore function    

    

Connect with us!

Thanks to CacheFly for the bandwidth for this show.