7

(avery) #1
FORGE

we’re going to use this display to create a real time
side-scrolling histogram, so you can see changes in
temperature over time with a simple glance.


CODE
We’re going to use two new libraries. The first
is the equivalent of the DHT library, only for the
screen. This allows us to easily access the hardware
without needing to understand or reverse-engineer
the protocol it uses to speak to the Arduino. The
wonderful Adafruit provides this library, and it’s
called Adafruit_SSD1306. The second library is
also from Adafruit, Adafruit_GFX, and provides a
collection of graphics ‘primitives’ for drawing things
like lines, rectangles, and text without needing to
write the algorithms ourselves. Both libraries can
be installed by opening the library dialogue from the
Arduino IDE (Sketch > Include Library > Manage
Libraries...), searching for the library names, and
clicking ‘Install’ from the correct result.


Before we dive into writing our own code,
we need to edit the header files of the
Adafruit_SSD1306 library. Without this edit, our
screen would only display every other line, and
this is because the header is hard-coded to use a
display resolution of 128 × 32 rather than 128 × 64.
To change this, open Adafruit_SSD1306.h (usually
found in Arduino/libraries/Adafruit_SSD1306)
and uncomment #define SSD1306_128_64 by removing
the first two forward slashes (on line 73 in our
version). Add two slashes to the beginning of the
#define SSD1306_128_32 line to comment out the old
resolution and save the file. Your code should look
like the following:


#define SSD1306_128_64
// #define SSD1306_128_32
// #define SSD1306_96_16

With that out of the way, let’s start our own new
project. Although the skeleton of the code is similar
to the previous tutorial, we’re going to be changing
most of the implementation. At the top of the file,


We’re going to use a couple
of new libraries in this tutorial
to do some magical stuff that
would otherwise take a year’s

worth of tutorials




WIRING


One of the nice things about the
SSD1306 display we’re using, and
many of its derivatives, is that it pushes
straight into your breadboard without
requiring any additional jumpers. The
signal carried by each of its four pins
is annotated across the top of the
screen, and this means you can still
see which pin does what, even with the
board plugged in. This is particularly
important because you need to pay
attention to which pin carries the
power (usually labelled VCC) and which
is for ground (GND). Get these the
wrong way round and you may break
the screen, your Arduino, or both.
You also need to check that power
requirements for your board match the
Arduino – ours is 3 V ~ 5 V DC. Power
needs to be connected directly to 5 V
on the Arduino and ground to the GND
adjacent to this on the Arduino.
However, we also need to connect
the temperature and humidity sensor
to the same power pins. The best way
of doing this is to use the power and
ground ‘rails’ on a typical breadboard.

Two are usually found on the outer edge
of each long side of the breadboard,
and connecting 5 V from the Arduino
to one of these and GND to the other
will deliver the power and ground to
any pin connected across the length
of the rail. With those connections in
place, it’s then as simple as making one
connection from the 5 V rail to VDD on
the screen and another from the 5 V rail
to VCC on the sensor, and the same must
be done for both GND pins.
The screen and the Arduino talk
to each other using the I^2 C protocol,
and this requires the use of specific
pins on the Arduino. These two pins,
normally labelled SCL and SCA on the
screen, need to be connected to the
corresponding pins on your Arduino,
and these can be different depending
on which Arduino you’re using. As
we’re using an Uno R3, SCL is analogue
pin 5 and SCA is analogue pin 4. Finally,
the data pin on the temperature and
humidity sensor is connected to digital
pin 2 on the Arduino, as it was in the
previous tutorial.

Above
The screen and sensor share the same 5 V
and GND rails on the breadboard
Free download pdf