Arduino’s eye, or about a photoresistor, not Tolkien’s mythology

Arduino’s eye – sounds like a quote from J.R.R. Tolkien, while it’s about connecting photoresistor to Arduino. With this article I would to begin short course dealing with basics of electronics.

Photoresistor is a element whose resistance is dependent of amount of light falling on it. What only remains is to measure this resistance. In which way?

For a playful use we will ignore accuracy, assuming simplified model.

How it looks?

Simple circuit built on breadboard (click on the picture to see notes on Flickr)

Circuit diagram looks like the following:

Circuit diagram

What is depicted here is so called voltage divider. You can read on Wikipedia, how it works.

Briefly speaking, voltage on the junction point between two resistors will be varying according to resistance of the photoresistor. And this is what we will be measuring with our program.

This is its code:

```// pin, from which we read the voltage
int photoPin = 0;
int val = 0;

//initial configuration
void setup()
{
pinMode(photoPin, INPUT); // setting the pin as input
Serial.begin(57600);
}

void loop()
{
//reading and standarizing to range 0-7
for (int i=0;i<=val;i++) {
Serial.print(".");
}
Serial.println();
delay(90);
}```

Program (as hardly every on Arduino) consists of the section preparing the device to work (`setup`) and main loop (`loop`) evoked as long as the power is available…

In main loop the voltage is measured, then the reading is converted in range 0-7 (because Arduino reads voltage as a number from 0 to 1023, so dividing it by 128 and rounding down gives digit from 0 to 7) and appropiate number of diodes is lighted up. Short break, and loop again…

The result:

Arduino IDE during execution of our program

While the program is running, I’m covering the photoresistor with my hand, changing its resistance, and thus the voltage being read by Arduino from pin 0. The number of dots is proportional to the reading.

Part list:

Coming soon – how to physically show varying reading.

LED diodes – how to flash and light

Recently there was silence on Starter Kit, because before holidays I was preparing to Bootstrap 9.4, what took most of my free time. Now, taking advantage of free day I’m making up for it.

Previously, we were dealing with reading from external sensor (photoresistor). Now we will try to show this reading.

We will use 7 LED diodes arranged in a line making a kind of indicator – the more light falls on the photoresistor, the more diodes will light. Beginning from the end – how it looks in action:

Forgive poor quality, but mobile phone camera is the only tool I can use to perpetuate my experiments with Arduino.

But despite of that the effect is visible. Behind the frame I zoom in and out the photoresistor to a lamp as the diodes are lighting up and fading out.

Arduino – play!

A long silence on the blog is just broken! During the last few months I was strongly engaged in projects not connected with Arduino, whole time was absorbed by keeping the Arduino shields shop, servicing customers, searching for new suppliers, etc.

Now I have a bit time which I can spend by documenting experiments with Arduino, thus I hope to add some examples with Starter Kit soon.

Silence, go away!

As we have to break the silence, so więc let’s begin from controlling so called buzzer a tiny speaker able to produce not very loud sounds, with the Arduino. Let’s start from final product:

Buzzer is controlled by Arduino, playing simple melody. Playing speed depend on reading from photoresistor (the more light, the slower it plays).

I wrote, how to read values from photoresistor in one of previous posts. Buzzer is stimulated to play by properly applied high and low voltage. For (Arduino output port) safety a 220 Ohm resistor is added to lower the current passing through (from PDF with documentation appears that it’s not obligatory – at least 14 Ohm resistance should not generate current higher than 36 mA – lower than the limit value for Arduino I/O port – 40 mA).
Thankfully Arduino is open source and there are many ready to use elements. So, playing melody (note) issue is described in Melody tutorial. We will take advantage of it and add only part reading value from photoresistor and converting it to adequate delay.

The only modification of Melody tutorial (except from `setup`) is a line in `loop`:

` tempo=analogRead(analog)/3+100;`

It divides the voltage reading by 3 and adds 100. This way we will obtain value in range from 100 to 1023/3+100=441. Original `tempo` was delay 300. Only the schematic remains:

The device schematic

For beginners – a detailed description, how to connect the wires:

Physical connections

Detailed part list (Fritzing has only piezo buzzer in basic library, so it is used on the schematics):

```Fritzing Bill of Materials

Sketch: 	buzzer.fz
Date: 	Tue Sep 29 01:11:59 2009

Arduino1      Arduino Diecimila
J1            Piezo Speaker
R1            220 Ω Resistor
R2            10k Ω Resistor
R3            Basic Photo-Resistor (Photocell)

Shopping List

Quantity	Part

1		10k Ω Resistor
1		220 Ω Resistor
1		Arduino Diecimila
1		Basic Photo-Resistor (Photocell)
1		Piezo Speaker
1		Tiny Bre```

And finally – the code used in example: melody.zip

PS
How do you like the diagrams and pictures designed in Frizting? For me, it’s perfect software for documenting experiments with Arduino. I reccommend – check it.

How to measure temperature with Arduino and MCP9700

One of the sensors included in Arduino Starter Kit is a temperature sensor. It’s analog device which doesn’t need any additional elements to work (it’s exactly MCP9700-E/TO). So, briefly speaking, after connecting ground and power supply we are already able to measure temperature. On the sensor’s page in Nettigo shop there is PDF datasheet in Files bookmark. So, let’s begin from terminals:

MCP9700 pinout

Important – the terminals are showed from the bottom of the sensor. To the first we connect supply voltage (both 3.3V and 5V from Arduino would be OK), ground (GND) goes to the third and the second terminal should be connected to Arduino Analog0 pin. As we can read from the datasheet (first page), sensitivity equals 10 mV/ºC. On the second page we have Output Voltage, 500 mV for 0°C. That’s all we should know to write a simple program:

```float temp;

void setup() {
Serial.begin(57600);
};

void loop () {
temp = temp - 0.5;
temp = temp / 0.01;
Serial.println(temp);
delay(500);
};```

The program measures temperature and sends the reading to Serial, from which we can read it using serial port monitor in Arduino IDE. How the temperature is calculated? I divded the whole process in three steps to make it easier to understand:

1. `temp = analogRead(0)*5/1024.0;` Reading value from analog input and converting it to voltage. Maximal voltage Arduino is able to measure equals 5V and the A/D converter’s resolution is 10 bits, which means 1024 values, thus voltage value on Analog0 input is the value returned by analogRead multiplied by the voltage equaling one step of A/D converter. You have to remember, that dividing 5 by 1024 would be interpreted by compiler as integer operation and the result in this case will equal 0. Because of this in the code appears `1024.0` and thanks to this entry, the compiler will treat the dividing as floating point operation.
2. `temp = temp - 0.5;` Calibrating to 0°C – the difference between voltage read from the sensor and 500 mV is linearily dependent on temperature.
3. `temp = temp / 0.01;` This difference is divided by 10mV/step and now we have temperature

As you can see, the MCP9700 sensor is very simple to use and beginners-friendly. Now everyone can measure temperature with Arduino.

When it lacks of current

Sooner or later in experiments with Arduino we will want to drive some kind of device, which needs more than 40 mA to work (40 mA is limitary value, because it’s maximum current that Arduino digital output can supply) It may be a relay, lighting bulb or other current-greedy device.

What to do?

We can use a transistor as an amplifier. Analog techniques is very broad issue, which I don’t know very much (everyone who has studied with me know what I mean :) ), but here I will attempt to present a few informations about driving devices with higher current demand. This example is based on materials from Arduino Starter Kit available on Nettigo.eu.

What you have to know about transistors.

Types – regarding of structure. Basic types are bipolar and field-effect (unipolar). We will focus on the bipolar. They have two groups: NPN and PNP. The difference in structure is other arrangement of semiconductor. Difference in using them is other way the current flows in the transistor, so there are two configurations of the supply voltage.

It seems easier to use NPN transistors with Arduino, because they need to be polarised with positive voltage that Arduino provides on digital output.

NPN type bipolar transistor symbol

Bipolar transistors can work in many applications and each of them can be used in other way. We need to switch high current with low current from Arduino output. To precise, the application we will use is called transistor switch. In this application the transistor is switched between two states: saturation and cutoff.

• cutoff: UBE is too low to bias the base-emiter junction. Thus, no current flows through the collector.
• saturation: such a large current flows into the base, that the law IC=hFE*IB isn’t true anymore. In this case, UCE voltage hardly reach zero.

Virtually, the collector current is limited only by the load – what’s connected between the collector and the ground. Current efficiency of our power supply also is a limitation. You must also be careful to not exceed the maximum allowed collector current, which, for currently supplied with Starter Kit MPS2222A transistor equals 600mA.

How it looks in practice?

If we supply a 1 mA current to base, the maximal collector current will be 35 mA (1mA * 35). In case of 10 mA – IC will be max. 350 mA. If IB equals 20 mA, IC will be max…. 600 mA. 600, not 700, because 600 mA is maximum collector current of MPS2222A.

It is worst case, because hFE may vary for different copy of transistor, for one can equal 35, for another 40 – then maximum transistor current 600 mA will be achieved already for 15 mA IB.

Before sketching the circuit – rules of polarising NPN transistor to work as switch:

• emitter connected to ground
• positive voltage on base (switch is on) or 0 (switch is off)
• collector connected to supply voltage through load

So:

NPN transistor - using method

When we close S1 switch (it equals setting Arduino digital output in high state) UCB will be 0.6V, a current starts to flow into the base. It must be high enough to saturate the transistor, so UCE must fall below UCE(sat) (collector-emiter junction saturation voltage). According to the datasheet UCE(sat) value depends on IC and IB currents. We can estimate it as about 0.3V.
In Arduino Starter Kit there isn’t any part, which could receive current higher than 40 mA, but we can join parallelly some LEDs to demonstrate this issue in practice.

Example of using 4 diodes to exceed 40 mA load

Let’s estimate current flowing through one diode – voltage drop between collector and emitter in saturation state is 0V, voltage drop on red diode – about 1.8V so on the resistor – 5-1.8 = 3.2. For resistors R2-R5 of value 280 Ohm the current flowing through them should be 3.2/280 = 11.42 mA. For 4 diodes it should give about 45 mA.

Before connecting the transistor we should know which terminal is which:

MPS2222A - pinout

1. emitter
2. base
3. collector

Physically connections illustrated in Fritzing (frankly admitting – this time it won’t be easy to read. The images probably will be better illustration):

Physically connected circuit

And two photos (on the left of breadboard there is other circuit assembled, so everything what is visible under 25th row doesn’t matter):

Physical construction - take one

Physical construction - take two

Assembly warning – cutted off resistor terminals are perfectly suitable for making short jumpers on breadboard – they are clearly visible in the picture.

Bill of materials generated from Fritzing (all the required parts are included in Arduino Starter Kit from Nettigo):

```Fritzing Bill of Materials

Sketch: 	tranzistor.fz
Date: 	Mon Dec 14 10:30:33 2009

Arduino       Arduino Diecimila
LED1          Red LED - 5mm
LED2          Red LED - 5mm
LED3          Red LED - 5mm
LED4          Red LED - 5mm
Q1            NPN-Transistor
R1            220Ω Resistor
R2            220Ω Resistor
R3            220Ω Resistor
R4            220Ω Resistor
R5            220Ω Resistor

Shopping List

Quantity	Part

5		220Ω Resistor
1		Arduino Diecimila
1		NPN-Transistor
4		Red LED```

Of course, 220Ω resistors are the ones supplied with Arduino Starter Kit for LED diodes and may have other values – from 200Ω to 280Ω. Regardless of the values, this circuit will work.

Sketch for Arduino is classical example of Blink:

```/*
*
* The basic Arduino example.  Turns on an LED on for one second,
* then off for one second, and so on...  We use pin 13 because,
* depending on your Arduino board, it has either a built-in LED
* or a built-in resistor so that you need only an LED.
*
*/

int ledPin = 02;                // LED connected to digital pin 13

void setup()                    // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
{
digitalWrite(ledPin, HIGH);   // sets the LED on
delay(4000);                  // waits for a second
digitalWrite(ledPin, LOW);    // sets the LED off
delay(2000);                  // waits for a second
}```

At the end, the values measured in real circuit, for 280Ω resistors:

• supply voltage – 4.96 V
• UCE voltage – 0.03 V
• voltage drop on lighted LEDs – from 1.86 to 1.93 V
• current flowing through LEDs (without incorporating real resistance R2-R5, each of them has 5% tolerance) – from 10.7 mA to 10.9 mA

As homework – it is possible to change pin used to drive transistor to any supporting PWM and control brightness of the diodes using `analogWrite` instead of `digitalWrite`. Movie illustrating such a modification:

Today I wrote slightly long article, but I had to include some theory here. I tried hard to avoid inaccuracies, but analog techniques aren’t my strong point :) If you notice any errors, please let me know, I will try to correct them.

Shifted LEDs

The last piece of Arduino Starter Kit, which still remains undescribed is small, but powerful integrated circuit – shift register. It is 74HC595, SIPO type (Serial-In, Parallel-Out) shift register with 8 parallel outputs.

Shift register has serial input and some (usually 8) parallel outputs. What it means? With every clock impulse the output states are shifted by one position. So, logic level of the 1st output appears on the 2nd one. Content of the 1st output is changed depending on serial input content.

How can we take advantage of this?

We should consider using shift register in every place which needs many logic outputs. You can say that three pins can control many more outputs. Let’s see it on the simplest example – LED diodes.

Let’s begin from wiring it up:

Circuit diagram with shift register - assembled on breadboard

Power is spreaded by rails on the breadboard. On the left board the LEDs are connected with resistors. On the right one is connected the 74HC595 itself.
Its pinout:

74HC595 pinout

On the preceding diagram is shoved the pinout according to datasheet from manufacturer (ST Microelectronics), whose chips are in offer of Nettigo and are part of Starter Kit. On the Arduino site there is description suitable to ICs marked according to Philips nomenclature. What it means? Fortunately the only difference is signals naming (for example, outputs – in STM: QA to QH, in Philips version: Q0 to Q7), placing and functions of pins is the same. In this tutorial I will be using naming compliant with STM.

The simplest connection between Arduino and shift register looks like the following:

• +5V power to register pin 16
• Ground to register pin 8

If we don’t mind the fact that after turning the power on the LEDs may light in random configuration and we want to use less digital outputs, we can connect it like this:

• G – register pin 13 to ground
• SCLR – register pin 10 to ground

The minimal configuration to control a shift register from Arduino is (all Arduino pins are digital inputs):

• SI – register pin 14 to Arduino pin 11
• SCK – register pin 11 to Arduino pin 12
• RCK – register pin 12 to Arduino pin 8

Between RCK pin and ground there should be capacitor (1 μF – not supplied with Starter Kit). A number of experiments I made prove that it isn’t necessary :)

Here is logic diagram of the connection:

Logic diagram of the connection

What remains? The code:

```//**************************************************************//
//  Name    : shiftOutCode, One By One                          //
//  Author  : Carlyn Maw, Tom Igoe                           //
//  Date    : 25 Oct, 2006                                      //
//  Version : 1.0                                               //
//  Notes   : Code for using a 74HC595 Shift Register           //
//          : to count from 0 to 255                            //
//****************************************************************

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

//holder for infromation you're going to pass to shifting function
byte data = 0;

void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

}

void loop() {

//function that blinks all the LEDs
//gets passed the number of blinks and the pause time

// light each pin one by one using a function A
for (int j = 0; j < 8; j++) {
lightShiftPinA(j);
delay(1000);
}

// light each pin one by one using a function A
for (int j = 0; j < 8; j++) {
lightShiftPinB(j);
delay(1000);
}

}

//This function uses bitwise math to move the pins up
void lightShiftPinA(int p) {
//defines a local variable
int pin;

//this is line uses a bitwise operator
//shifting a bit left using << is the same
//as multiplying the decimal number by two.
pin = 1<< p;

//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, LOW);
//move 'em out
shiftOut(dataPin, clockPin, MSBFIRST, pin);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, HIGH);

}

//This function uses that fact that each bit in a byte
//is 2 times greater than the one before it to
//shift the bits higher
void lightShiftPinB(int p) {
//defines a local variable
int pin;

//start with the pin = 1 so that if 0 is passed to this
//function pin 0 will light.
pin = 1;

for (int x = 0; x < p; x++) {
pin = pin * 2;
}

//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, LOW);
//move 'em out
shiftOut(dataPin, clockPin, MSBFIRST, pin);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, HIGH);

}

//blinks the whole register based on the number of times you want to
//blink "n" and the pause between them "d"
//starts with a moment of darkness to make sure the first blink
//has its full visual effect.
void blinkAll(int n, int d) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
delay(200);
for (int x = 0; x < n; x++) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 255);
digitalWrite(latchPin, HIGH);
delay(d);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
delay(d);
}
}```

This will light all the diodes, then light every diode one by one, flash all the LEDs twice and light again using another method.

The most important informations:

• shiftOut is a function from Arduino library, serially sending selected byte to specified output
• we can send data to the register by setting RCK (connected to Arduino pin 8) in low state