Friday, November 6, 2015

Plotting Light Levels With Arduino 1.6.6

Long ago in a galaxy far away, we built a simple CdS Light Sensor (Sometimes called a LDR). Now with the real time plotting capability of Arduino 1.6.6, it's even more fun! The original project requires no changes, just pick Serial Plotter from the Tools menu.

CdS Photoresistor -
10k Resistor -

Code -

Plotting Temperature -

Thursday, November 5, 2015

Plotting Temperature with Arduino 1.6.6

One of the new features of Arduino1.6.6 is the ability to plot data. In this example, I have taken a DHT-11 temperature sensor, and I'm sending the data to the serial plotter (found on the Tools menu). You can only have one axis of data, so I have a single serial.println in the code.

I'm using a DHT-11 module that comes on a breakout board with the resistor all ready added. You will need the DHT library found at I plugged the module directly into the Arduino, so the data pin is connected to Arduino pin 6, - is connected to Arduino Pin 7, and +5 is connected to pin 5. You will see in the code where I'm sending digitalWrite commands to pins 5 and 7 sending a high and a low respectively, powering the module. Because I'm taking a reading every 2 seconds (it's a slow sensor) the serial plotting crawls very slowly across the screen.

#include "DHT.h"

#define DHTPIN 6     // what digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)


void setup() {
  pinMode(7, OUTPUT);  
  pinMode(5, OUTPUT);  
  digitalWrite(7, LOW);
  digitalWrite(5, HIGH);
  //Serial.println("DHTxx test!");


void loop() {
  // Wait a few seconds between measurements.

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)

  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

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


Monday, November 2, 2015


The typical LCD used with an Arduino has 16 pins. only 6 of them have to use a I/O port on the Arduino. But what if you could get that down to two I/O pins, and still have those pins available for other devices? The I2C interface is on pins A4 and A5 of the Arduino. These have addresses, and are therefore shareable with other I2C devices that have different addresses.

Now, you can buy I2C LCD's, and you can even find I2C LCD's with different addresses, but they are typically two line LCD's, and the addresses are fixed. I'm going to show you how to build your own I2C interface, select one of 8 addresses, and even be able to add up to 8 inputs, using a MCP23017 16 port expander chip.

This is the same chip Adafruit uses on their I2C LCD Keypad Shield, and uses their library to talk to it. First let's look at the wiring. The connections between the Hitachi type parallel LCD (2 or 4 line) and the MCP23017 are shown below. Pins 15, 16, and 17 on the MCP23017 determine the I2C address. We have all 3 grounded, as this is the default address the Adafruit library uses. To add multiple displays, or pick another address, the library will have to be modified, so we will go with the default in the this example.

Notice the 220 Ohm resistor on the LCD k (cathode, gnd) connection. This is a MUST! Without it, you can blow the backlight pin on the MCP23017.

SDA (pin 13 on the MCP2307) connects to Arduino A4 and SCL (pin 12 on the MCP2307) connects to Arduino A5.

Download and install the Adafruit library, run the example "Hello  World", and this lcd interface is operational. We will address adding inputs to this chip, and editing the library address so that up to 8 MCP23017 chips can be used in a future article. Email me with questions.

Address hint:
Adafruit_MCP23017.h contains 
#define MCP23017_ADDRESS 0x20
Pin 17 = A2, Pin 16 = A1, and Pin 15 = A0
0 = ground, 1 = +5v
The address format is 0100A2A1A0, so since we have grounded all 3 lines, we are using binary 0100000, or 20 in hex. 0100111 would be 27 in hex.

Now figure out how to pass the address when initializing MCP23017.h ......

So, what is next? 16 channel relay boards with jumpers for address selectors? Maybe seven segment LED controllers?