## Balancing robots – for dummies?

Balancing robots are those, which can maintain balance by themselves. And how it looks? For example like this.

Author of the robot above began a interesting thread on Arduino forum: Balancing robot for dummies, where he promises to describe everything in a simple and aproachable way. If you are interested in this kind of robots – trace this thread with attention.

You see that vortex? It’s officially called LCD kits testing station. Just after soldering a module it needs to be connected and examined, if it lights and displays everything properly. There could be a silly text, but it would be too simple.

Therefore among this cable clutter is hidden temperature sensor MCP9700 (actually two) and the LCD simply displays readings from it. Last night I somehow became interested in the temperature Arduino was displaying. It was definitely too high. The difference between 21 and 24 degrees is well distinguished by everyone (the photo was taken at other temperature conditions :) ). There’s no way to blame inaccuracy of measurement for it, because the sensor yet has +/- 2 deg. accuracy.

Moreover, once or twice during a talk with my clients I heard opinions that the sensor shows too much. For this reason I began to grub around and try variety of things.

### How voltage converter works

Analog-digital converter is fully described in ATmega328 datasheet on page 250.
In a nutshell: AREF voltage is the voltage powering DAC converter (digital controlled voltage divider) which output is connected to a comparator matching AREF to the voltage on a certain analog input. DAC voltage “starts” from zero and increases until it not excesses measured voltage level.
Thanks to it the measured voltage value is known.

Accuracy of such a measurement stands out from the very beginning. The first thing is resolution (in bits) of DAC. In other words – with which step the voltage is increased. ATmega328 has a 10-bit one.

The second value is AREF voltage. When the number of steps is defined (1023, because of 10 bits), the voltage per step is known. It’s AREF/resolution. ATmega can utilize 3 different AREF sources. It could be internal 1.1V source (don’t know the accuracy), supply voltage (about 5V) or a value given to AREF pin.

Knowing this value is essential to convert digital reading from A/D to a real voltage value. Let’s skip the internal 1.1V source and external 5V AREF for now. We will focus on default AREF value – 5V.

Now, basic knowledge about MCP9700 sensors, exactly about sensitivity, would be useful. It equals 10 mV/deg., so if we want to read what exactly the sensor emits, we have to know the AREF value precisely.

### Maybe by an example

Reading from analogRead() has given us 150. We think that the voltage read is 5/1024*150 = 0.7324 V. It’s true, but if AREF equals 5V in fact. And what if it’s 4.9V? Then the real value meant by reading of 150 equals: 0.7178V. The difference – 14.6 mV. For the sensitivity of 10mV/deg. – almost 1.5 degrees!

And how much exactly AREF equals in default setup? Supposedly 5V, but in fact Vcc (supply voltage) provided to ATmega328.

Wait, someone could say Arduino has built-in 5V voltage regulator, so such a high difference shouldn’t exist. It’s true, but only when Arduino is powered by external source, not USB!

As is well known, USB standard tells that supply voltage is 5V, so the voltage regulator doesn’t work, the voltage from USB goes directly to ATmega. The regulator doesn’t work because it needs suitably higher voltage in order to have a “reserve”, so it’s technically impossible to maintain 5V while voltage supplied to the regulator also equals 5V.

In other words, we have to rely on what the computer provides on USB. And here I became suprised, because my notebook gives 4.9V instead of 5V on USB.

Finally – because of that my reading was increased by those 1.5 degrees.

However, when using analogReference(INTERNAL) we will abandon upper ranges of the reading (because maximum reading is 1.1V, what after converting to temperature gives (1.1-0.5)/0.01 = 60 degrees) in order to increase readout accuracy (1.1/1024 = about 1mV – 0.1 degree). Note once again – it’s increasing readout, not mesurement accuracy :) .

That was briefly about what exactly analogRead measures. And, in order to explain why on the photo one readout is 18 degrees, while the another – 10. This isn’t a trick with analogRead – one of the sensors is MCP version A, the another – regular MCP. The one with “A” (supplied with Starter Kit and also sold separately) has +/- 2 accuracy, without “A” – +/- 4 degrees.

## Tilt sensor

Nettigo offers tilt sensors with 30° and 60° (exactly from -30° to +30°) operating range.
Tilt sensor is a kind of switch. The difference between it and regular switches is that the current starts to flow through it when it’s tilted. Such a sensors are commonly user in car alarm system They are monitoring car’s position and triggering the alarm when the car is dragged by a wrecker truck, for example.
It looks like this:

### Hardware

Utilizing the sensor isn’t difficult. It’s similiar to every ordinary switch. Only a free Arduino digital pin is needed. When the pin is in input mode, its state remains unknown (random) until a certain voltage is applied to it. Applying a “GND” sets it into “LOW” state, and providing 5V causes “HIGH” state.

If we have the sensor wired to a digital pin and “GND”, we know the state will switch to “LOW” when the current will begin to flow. When the current doesn’t flow, the state is going to be reversed – “HIGH”. Presently, the state is undefined. Thus, to define the state we will use built-in “Pull Up” resistor. It causes setting a pin into “HIGH” state when no voltage is applied to it.

### Software

We’re taking advantage of the sensor reading the state of a selected digital pin. First, we must set the pin as “INPUT” in “setup” section.

``````pinMode(pin_number, INPUT);
``````

And activate internal “Pull Up” resistor.

``````digitalWrite(pin_number, HIGH);
``````

After that, in “loop” function you can read pin’s state and thus the sensor’s position with “digitalRead” function.

``````variable = digitalRead(pin_number);
``````

Example program, lighting the “L” diode up during a tilt.

```// Defining a pin for the tilt sensor
#define SENSOR_PIN 2
// Defining a pin for the LED diode
#define LED_PIN 13

void setup()
{
// Setting sensor's pin as input
pinMode(SENSOR_PIN, INPUT);
// Turning internal Pull Up resistor on
digitalWrite(SENSOR_PIN, HIGH);
pinMode(LED_PIN, OUTPUT);
}

void loop()
{
// Checking sensor's state
{
// Action if off
digitalWrite(LED_PIN, HIGH); // Turning the LED on
}
else
{
// Action if on
digitalWrite(LED_PIN, LOW); // Turning the LED off
}
}```

Assembled module

As every man well knows, a woman holds in her purse only treasures. These are so valuable, that no-one is allowed to browse it. In order to protect poor ladies from nosy people I resolved to build a purse alarm.

#### Elements

The following parts will be needed:

• Tilt sensor for sensing the position of the purse
• Piezo buzzer for alarming

I assembled the elements as on the following schematic. I used pin 2 as sensor’s input, pin 3 as output for piezo buzzer and pin 13 for LED diode.

The code isn’t much different from the previous, except from additional sound procedures.

First of them is “tone”, turning the sound on.

``````tone(speaker_pin_number, frequency);
``````

The second – “noTone”, turning it off.

``noTone(speaker_pin_number);``
```// Defining a pin for the tilt sensor
#define SENSOR_PIN 2
// Defining a pin for the LED diode
#define LED_PIN 13
// Defining a pin for the piezo buzzer
#define PIEZO_PIN 3

void setup()
{
// Setting sensor's pin as input
pinMode(SENSOR_PIN, INPUT);
// Turning internal Pull Up resistor on
digitalWrite(SENSOR_PIN, HIGH);
pinMode(LED_PIN, OUTPUT);
}

void loop()
{
// Checking sensor's state
{
// Action if on
digitalWrite(LED_PIN, HIGH); // Turning the LED on
tone(PIEZO_PIN, 600); // Alarm sound
}
else
{
// Action if off
digitalWrite(LED_PIN, LOW); // Turning the LED off
noTone(PIEZO_PIN); // Turning alarm sound off
}
}```

It’s easily noticeable that the alarm is too sensitive and every shock (even foot stamping) causes “screeching” of the buzzer. This would not appeal to the purse’s owner.

### What is the difference between shocks and poking around in a purse?

The easiest way to distinguish them is switching speed of sensor. Shakes are switching it in thousandths of a second, while moves caused by a lifter – from tenths of a second to a few seconds. So, the time in which the sensor turns on must be counted and, if it’s longer than 0.1 s (moving the purse), the alarm should be triggered.

For this case, I used “millis” function, which returns how many miliseconds have elapsed from turning Arduino on. It could be applicated to storing the time from turning the sensor on. When a previously set value is reached, the alarm is turned on.

```// Defining a pin for the tilt sensor
#define SENSOR_PIN 2
// Defining a pin for the LED diode
#define LED_PIN 13
// Defining a pin for the piezo
#define PIEZO_PIN 3
// Defining sensitivity time
#define SENSOR_TIME 100

// Variable storing the switching time
unsigned long sensor_time;

// Function invoked, when the alarm is on
void alarm_on()
{
digitalWrite(LED_PIN, HIGH); // LED on
tone(PIEZO_PIN, 600); // Alarm sound
}

//  Function invoked, when the alarm is off
void alarm_off()
{
digitalWrite(LED_PIN, LOW); // LED off
noTone(PIEZO_PIN); // Turning alarm sound off
}

// Function checking sensor's state
void check_sensor()
{
// Checking sensor's state
{
// Action if off

if (sensor_time == 0) // Checking if the state was changed
{
sensor_time = millis(); // Storing the sensor's activation time
}
// Checking if the state of sensor has been lasting for a specified time
else if (millis() - sensor_time > SENSOR_TIME)
{
alarm_on();
}
}
else
{
// Action if on
sensor_time = 0;
alarm_off();
}
}

void setup()
{
// Setting the sensor's pin as input
pinMode(SENSOR_PIN, INPUT);
// Turning internal Pull Up resistor on
digitalWrite(SENSOR_PIN, HIGH);
pinMode(LED_PIN, OUTPUT);
}

void loop()
{
check_sensor();
}```

### Real alarm

Our purse alarm has one more disadvantage. It turns off immediately when the purse returns to its position. This is not a way real alarm works!
Professional alarm systems also have varied sounds. Here we will use “millis” function again. In the first place we are going to count, how much time was elapsed since triggering the alarm. It will be set off only when a certain amount of time will elapse, in order to deter and attract attention more effectively.
In the second place I employed time measuring to check the duration of signal tone, so as to switch it cyclically between high and low pitches. This way our purse alarm will be better distinguished from other noises.

```// Defining a pin for the tilt sensor
#define SENSOR_PIN 2
// Defining a pin for the LED diode
#define LED_PIN 13
// Defining a pin for the piezo
#define PIEZO_PIN 3
// Defining sensitivity time [ms]
#define SENSOR_TIME 100
// Defining alarm duration time [ms] (5000 ms = 5 s)
#define ALARM_TIME 5000

// variable storing sensor's switching time
unsigned long sensor_time;
// variable storing alarm duration
unsigned long alarm_time;
// variable storing pitch change time
unsigned long signal_time = 0;
// variable storing the pitch value
// 0 - no sound
// 1 - first tone
// > 1 - second tone
byte sound_tone = 0;

// alarm siren function
void alarm_signal()
{
if (sound_tone != 0) // checking if the siren should be turned on
{
if (millis() - signal_time > 750) // checking tone duration
{
signal_time = millis(); // storing the time when the tone was turned on
sound_tone = ~sound_tone; // turning on the second tone
}

if (sound_tone == 1) // first tone
{
tone(PIEZO_PIN, 600);
}
else // second tone
{
tone(PIEZO_PIN, 1200);
}
}
else
{
noTone(PIEZO_PIN); // Sound off
}
}

// Function invoked when the alarm is off
void alarm_on()
{
alarm_time = millis(); // storing the time when the tone was turned on
digitalWrite(LED_PIN, HIGH); // LED on
sound_tone = 1; // Turning alarm siren on
}

// Function invoked when the alarm is off
void alarm_off()
{
if (millis() - alarm_time > ALARM_TIME) // checking alarm duration
{
digitalWrite(LED_PIN, LOW); // LED off
sound_tone = 0; // Turning alarm siren off
}
}

// function checking sensor's state
void check_sensor()
{
// checking sensor's state
{
// Action if off

if (sensor_time == 0) // checking if the state was changed
{
sensor_time = millis(); // storing sensor's activation time
}
// Condition, if sensor's state lasts for a specified time
else if (millis() - sensor_time > SENSOR_TIME)
{
alarm_on();
}
}
else
{
// Action if off
sensor_time = 0;
alarm_off();
}
}

void setup()
{
// Setting the sensor's pin as input
pinMode(SENSOR_PIN, INPUT);
// Turning internal Pull Up resistor on
digitalWrite(SENSOR_PIN, HIGH);
pinMode(LED_PIN, OUTPUT);
}

void loop()
{
check_sensor();
alarm_signal();
}```

This is only one of many applications of tilt sensor. If you have any ideas or questions – leave a comment or use the mail.

Source codes:

During meetings with readers I found out that many of them like the Arduino project, but the greatest barrier is soldering.
Some are literally scared of this – their hands are trembling. In this article I would like to make soldering more familiar.

Some of electronic parts available as printed circuit boards with ready-to-use devices (modules) has already terminals in form of holes on the board. It only lacks of connectors or wires. For use with Arduino, the best idea is to solder pin header into these holes.

Pin header is a kind of connector with pins aranged in a line. In most cases the raster (gap between two pins) is the same – 2.54 mm (0.1 inch).

### Components

To solder we need:

#### Soldering iron

If you are amateur electronic-hobbyist, you don’t have to buy advanced and expensive tools. I myself am using simple and cheap 40W soldering iron from long time ago. It’s available in electronics shops for a few dollars. Of course, I reccomend a “pencil” type, not transformer. The second type is better for wires soldering than for small elements, so excluding them, you will avoid many problems.
The most important thing in using a soldering iron is to know, which end gets hot. Grabbing the unproper may end badly.
The iron heats approximately for 2 minutes. It’s ready when solder melts on the soldering iron’s tip.

#### Solder

Solder is a kind of wire made from tin and lead with specific chemicals added. Solder is usually available in a form of spool with wire in a sort of diameters.
I reccomend diameter up to 1mm. We should realize that the smaller is the diameter of solder, the smaller parts can be soldered. However, when diameter is too small, a considerable length of solder will be needed for soldering bigger parts, what could be uncomfortable.
I use 1 mm solder and in the lfollowing example it will be a bit too thick.

Pin headers are available in a sort of configurations (from one to a few rows). We will settle for one row. Usually they are sold in 50 pins pieces.
If a device requires less pins, the remaining are broken up.

### Preparing

Holes and soldering pads should be degreased with a cloth soaked with alcohol, solvent or water. Most of the manufacturers reccomend the last option, but you must be careful, because water can easily short the circuit an slowly vapors from thin gaps like places under ICs.

We are breaking up desired number of pins,

and trying on, whether they fit to the board.

### Further preparation

In fact, you could begin soldering right now, but yet one thing would be helpful – a “third hand”. There is quite difficult to hold a part in place, when we have soldering iron in one hand and solder in the second. Pin headers can’t be soldered straigthly (at 90° angle) when the module leans against it.
Fortunately, many ways to solve this problem exist. The most popular is magnifying glasses with “crocodile” grips tightened to a table.
Though, I used…

…a small vise.

The board shouldn’t be tightened by the vise directly. This could corrupt it, otherwise it wouldn’t be held stabily. So, I suggest to cover jaws of the vise with something like foam used to isolate pipes.

What to do with pin headers falling out from the board?
I suggest to glue the plastic ends to the board with “Super Glue”, for example.

### Soldering process

The soldering consists in putting hot soldering iron tip to the junction point. Next, solder is applied there. It must melt and flow down, filling the junction.

You have to remember not to heat one point too long (the soldered parts could be damaged). Manufacturers reccomend max 2-3 s.
If the solder will accidently flow over two pins, melt it and divide quickly, using something like a toothpick or match.

Properly soldered junction is cone-shaped.

The most common mistake in soldering is applying too less (the soldering pad isn’t well covered) or too much (there is a bead instead of a cone) solder to the junction.

### My soldering

To finally dispel the fear of soldering, I decided to record in on a video
As you can see, dear reader, this isn’t so difficult.

## Humidity sensor

Every amateur and professional meteo station requires humidity sensor to be completely happy. This sensor along with a termometer allows to forecast so called “dew point”. Dew point is a temperature at which (and at a certain humidity) vapor water condenses into water. Usually it produces dew on grass, but in specific conditions also allows to calculate cloud base or figure out why in the kitchen plaster falls out from the ceiling ;-).

### Capacitor

HCH-1000 available in Nettigo is a capacitive humidity sensor. Capacitive means that the sensor is a capacitor, which capacitance proportionally depends on the humidity. Average value is about 330 pF.

Capacitor is, in a simple explanation, an element which stores electric charge, or even simpler (electronics engineers – please forgive me) – some kind of very low capacity accumulator.

### Calculating the capacitance

Everyone, who had anything to do with an accumulator (even in a mobile phone) know that charging and discharging it takes certain amount of time. This amount depends on capacitance of the accumulator and its electric demand. The same rule concerns capacitors.

Professionally, it’s called RC circuit (because it consists of a resistor and a capacitor connected serially).

This graph shows the time, in which the voltage drop on the capacitor achieves a percent of supply voltage (V supply). It shows that 63,2% of the voltage will be achieved in one time unit, which can be calculated from the following equation:

t = RC

where:

• t – time in seconds
• R – resistance of the resistor in Ω
• C – capacitance of the capacitor in farads

Assuming the resistor is 10 MΩ and average capacitance of the sensor is 330 pF, charging to 63,2% of supply voltage will take:

t = RC = 10 MΩ * 330 pF = 10000000 Ω * 0,00000000033 F = 0,0033 s

It’s easier to calculate using units prefixes

t = 10 * 10^6 Ω * 330 * 10^-12 F = 10 * 330 * 10^-6 = 3300 * 10^-6 = 3300 µs.

Charging to higher parts of the supply voltage is, according to the chart, multiplication of this time. For example, for 95% it takes 3 times longer.

t = 3RC = 3 * 3300 * 10^-6 = 9900 µs

Discharging is reversed process in which correlation between the time and voltage value is like the following:

### Implementation in Arduino

The basic model of this circuit should perform the following steps:

• Charging the capacitor
• Checking charge level
• Measuring charging time
• Discharging the capacitor

The following picture shows a connection diagram of the basic model.

Pin 3 is responsible for charging the capacitor, pin 2 for discharging. Analog pin 0 measures voltage drop across the capacitor. To pin 3 is attached to a chrging resistor 10 MΩ, To PIN 2 – discharging resistor 220 Ω.
Capacitance measurement procedure is composed of two consecutive subprograms.

## Temperature and LCD

In Nettigo recently appeared LCD kit for Arduino – it is 2×16 alphanumeric LCD with soldered connector fitting to a breadboard and 10 kΩ linear potentiometer for contrast regulation.

The LCD module has HD44780-compatible controler, what means LiquidCrystal library will be able to handle it. How to connect it?

Let’s begin from the code:

```#include <LiquidCrystal.h>

float temp;
LiquidCrystal lcd (12,11,10,9,8,7);

void setup() {
analogReference(EXTERNAL);
};

void loop () {

temp = temp - 0.5;
temp = temp / 0.01;
delay(500);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print("        ");
lcd.setCursor(0,0);
};```

Basic LCD handling boils down to connecting (I’ll tell about it later) and initializing it:
``` LiquidCrystal lcd (12,11,10,9,8,7); ```

The code above defines `lcd` variable, which will be used for communicating with the module. We will assume the simplest form here, so we are wiring it up:

• RS pin of the module
• ENABLE pin of the module
• Data pins of the module: D4, D5, D6, D7

In this example 12 – RS, 11 – ENABLE, 10-7 D4-D7. Apart from this we need to connect R/W signal to ground (we won’t write anything to it) and power. Even if we’ve connected it all, the display shows 32 little black squares. We need also to set the contrast, providing voltage in range 0 – 5V to Vo pin of the LCD. To be able to adjust it, we will use a potentiometer included in the kit. Everything illustrated in Fritzing (the transistor works as MCP9700, because there isn’t such a part in Fritzing, package is the same – TO-92):

After that, in `loop` we are reading temperature form MCP-9700 (described in this post). Having the temperature already, we are showing Temp: text and the reading, and going back to the beginning.

## How to improve the temperature measurement?

Temperature reading (in form of voltage measured by analog input 0) is given by MCP9700 with accuracy dependent on voltage converter in Arduino. It’s 10-bit and measures voltage from 0 to 5V by default. 10 bits mean that we have 1024 steps in voltage reading. Thus, the voltage is measured with 4.88 mV (5/1024) resolution.

MCP9700 sensor has 10 mV / degree resolution, so it virtually means that we have 0.5 degree accuracy (it’s not about sensor’s quality, it’s about how Arduino measures the voltage).

Arduino’s A/D converter is able to be provided with external reference voltage (AREF – the peripheral pin in the digital outputs row), which replaces 5V. This voltage can be higher or lower than 5V EDIT: Unfortunately, I made a mistake here – converter’s datasheet tell that maximum reference value is the supply voltage – for Arduino it means 5V. If someone needs to measure higher voltage, then voltage divider always could be used…

WARNING!
Potential risk.
According to the reference of
analogReference function, the default setting (when analogReference isn’t invoked at all or invoked with DEFAULT value) could damage the microcontroller on Arduino board when using external AREF!

MCP9700 datasheet shows that maximum output voltage is 500 mV+ 125*10mV = 1.75V. It would be best to force the converter to measure voltage in range 0-1.75V, but it requires to add a voltage divider. This is not difficult, but in order not to complicate, we will use a simple measurement quality improving method, without any additional elements. We will exploit the fact that Arduino provides a 3.3V voltage, apart form 5V and we can improve measurement resolution without any problems.

Because of this in `setup` the analogReference(EXTERNAL) (it’s not enough to supply a voltage to AREF) function is invoked and in `loop` the tempreature calculating formula is changed. After changing the reference voltage we have 3.2 mV step, which means the temperature is measured with 0.3°C accuracy and not 0.5°C, like before.

NOTE – the improvement affects only the reading, not the measurement itself. The sensor still has +-2°C accuracy :) . This thick will improve only our mood, because the reading doesn’t jump so much :)

## Arduino and time, or DS1307

Arduino hasn’t internal clock. It means some operations connected with time (delaying for specified interval) are possible, but date and time controlling isn’t so easy.

To let Arduino know, what time is it exactly, there is external clock device needed. Such a device is Dallas DS1307, available on Nettigo.eu in form of Arduino-connectable module. This module has a battery back-up, so once set, the time isn’t deleted after shutting Arduino down.

## Assembly

The module is already soldered, but connecting to Arduino remains open issue. It looks like the following:

In the left upper corner there is 4-pin header socket with 2.54 mm raster. Unfortunately we still haven’t cables for this socket in stock. You have to scare it up or connect through ICSP socket using goldpins. Those elements are provided with the module, but need soldering (simple even for beginners).

Using the cable we can connect the module to any controler supporting I2C protocol.

ICSP socket (from which the module is powered) has two positions to which it could be soldered. The first is for Arduino Duemilanove and its clones (taking pinout and size into account) or Seeeduino Mega. With such a connection the module is incompatible with Arduino Mega (relative position of ICSp pins and I2C bus output is different).

Of course, it always remains to solder wires instead of goldpins and ICSP and then we are able to connect with every microcontroler supporting I2C. How the module with soldered socket and goldpins looks show the pictures below:

## Sonar

If you visit this blog and you have an Arduino, you probably dreamed about building your own robot. The simplest perform easy commands (turn over, move). Things will become more complicated, if we want to build more standalone robot. The one, that would intelligently find a way to destination and avoid collisions. Sounds unreal? Of course! But there’s nothing difficult, it only requires some practice and…
…sensors.

One of them is sonar, available in Nettigo. This sensor measures the distance to an obstacle using ultrasound (sound with a frequency about 42 kHz, what is greater than the upper limit of human hearing). Normally, this is used in alarm systems (unlike passive infrared systems there is possible to set the trigger distance) and in parking sensors. Installing it into your robot will let you know, how far are obstacles from the robot or make something like environment map.
Maximum distance detected by the sensor is 255 inches (6 meters) with 1 inch (2,54 cm) resolution. Soundwave beam width of “raw” sensor is about 24 inches (61 cm). The smallest distance sonar detects is 6 inches (15 cm).

EZ1 sonar has quite a few terminals, but you shouldn’t be afraid of their amount. Most of them are for your convenience. The first two (GND and +5) are for powering the sensor. We connect them to their equivalents on Arduino.

The remaining terminals provide variety of signals representing detected distance.

## 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?

Circuit diagram looks like the following:

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:

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.
Continue reading “LED diodes – how to flash and light”