Showing posts with label arduino. Show all posts
Showing posts with label arduino. Show all posts

Saturday, May 10, 2014

Arduino Project Lesson Four; DC Motors

Overview

In this lesson, you will learn how to control a small DC motor using an Arduino and a transistor.
learn_arduino_overview.jpg
You will use an Arduino analog output (PWM) to control the speed of the motor by sending a number between 0 and 255 from the Serial Monitor.

Parts

To build the project described in this lesson, you will need the following parts.

Part

Qty

learn_arduino_dc_motor.jpg
Small 6V DC Motor 1
learn_arduino_transistor.jpg
PN2222 Transistor 1
learn_arduino_diode.jpg
1N4001 diode 1
learn_arduino_R-270-level.jpg
270 Ω Resistor (red, purple, brown stripes) 1
learn_arduino_breadboard_half_web.jpg
Half-size Breadboard 1
learn_arduino_uno_r3_web.jpg
Arduino Uno R3 1
learn_arduino_jumpers_web.jpg
Jumper wire pack 1

Breadboard Layout

When you put together the breadboard, there are two things to look out for.
Firstly, make sure that the transistor is the right way around. The flat side of the transistor should be on the right-hand side of the breadboard.
Secondly the striped end of the diode should be towards the +5V power line - see the image below!
The motor that comes with Adafruit Arduino kits does not draw more than 250mA but if you have a different motor, it could easily draw 1000mA, more than a USB port can handle! If you aren't sure of a motor's current draw, power the Arduino from a wall adapter, not just USB
learn_arduino_breadboard.jpg
The motor can be connected either way around.

Arduino Code

Load up the following sketch onto your Arduino.  

/*
Lesson Four; DC Motors We will start make Line Following Robot 

*/
int motorPin = 3;
void setup() 
{
  pinMode(motorPin, OUTPUT); 
  Serial.begin(9600); 
  while (! Serial); 
Serial.println("Speed 0 to 255"); 
  void loop()  
 
  if (Serial.available()) 
  { 
    int speed = Serial.parseInt(); 
    if (speed >= 0 && speed <= 255) 
    { 
      analogWrite(motorPin, speed); 
    } 
  } 
The transistor acts like a switch, controlling the power to the motor, Arduino pin 3 is used to turn the transistor on and off and is given the name 'motorPin' in the sketch.
When the sketch starts, it prompts you, to remind you that to control the speed of the motor you need to enter a value between 0 and 255 in the Serial Monitor.
learn_arduino_serial_monitor.jpg
In the 'loop' function, the command 'Serial.parseInt' is used to read the number entered as text in the Serial Monitor and convert it into an 'int'.
You could type any number here, so the 'if' statement on the next line only does an analog write with this number if the number is between 0 and 255.

Transistors

The small DC motor, is likely to use more power than an Arduino digital output can handle directly. If we tried to connect the motor straight to an Arduino pin, there is a good chance that it could damage the Arduino.
A small transistor like the PN2222 can be used as a switch that uses just a little current from the Arduino digital output to control the much bigger current of the motor.
learn_arduino_transistor.png
The transistor has three leads. Most of the electricity flows from the Collector to the Emitter, but this will only happen if a small amount is flowing into the Base connection. This small current is supplied by the Arduino digital output.
The diagram below is called a schematic diagram. Like a breadboard layout, it is a way of showing how the parts of an electronic project are connected together.
learn_arduino_schematic.jpg
The pin D3 of the Arduino is connected to the resistor. Just like when using an LED, this limits the current flowing into the transistor through the base.
There is a diode connected across the connections of the motor. Diodes only allow electricity to flow in one direction (the direction of their arrow).
When you turn the power off to a motor, you get a negative spike of voltage, that can damage your Arduino or the transistor. The diode protects against this, by shorting out any such reverse current from the motor.

Friday, May 9, 2014

Arduino Project Lesson Three; LCD Display Part Two

Overview

In this lesson, you will build on what we have learnt in lesson 11 and use a LCD display to show the temperature and light intensity.  
learn_arduino_overview.jpg
Light intensity is measured using the same photocell that you used in lesson 9.
To measure the temperature, you will use a temperature measurement chip. This device has just three leads two for 5V and GND and the third lead is connected directly to an analog input on the Arduino.

Parts

Part

Qty

learn_arduino_lcd.jpg
LCD Display (16x2 characters) 1
learn_arduino_pot.jpg
10 kΩ variable resistor (pot) 1
learn_arduino_R-1k-level.jpg
1 kΩ Resistor (brown, black, red stripes) 1
learn_arduino_resistor_LDR.jpg
Photocell (Light Dependent Resistor) 1
learn_arduino_transistor.jpg
TMP36 temperature sensor 1
learn_arduino_breadboard_half_web.jpg
Half-size Breadboard 1
learn_arduino_uno_r3_web.jpg
Arduino Uno R3 1
learn_arduino_jumpers_web.jpg
Jumper wire pack 1
The TMP36 looks just like the PN2222 transistor, but if you look at the flat side of the package body, you should see it labelled as a TMP36.

Breadboard Layout

The breadboard layout is based on the layout from lesson 11, so if you still have this on the breadboard it will simplify things greatly.
learn_arduino_fritzing.jpg
There are a few jumper wires that have been moved slightly on this layout. In particular, those near the pot.
The photocell, 1 kΩ resistor and TMP36 are all new additions to the board. The TMP36 has its curved face towards the display.

Arduino Code

The sketch for this is based on that of lesson 11. Load it up onto your Arduino and you should find that warming the temperature sensor by putting your finger on it will increase the temperature reading.
Also if you wave your hand over the photocell blocking out some of the light, the reading will decrease.

/* 
Lesson Three; Light and Temperature 
*/ 
#include <LiquidCrystal.h> 
int tempPin = 0; 
int lightPin = 1; 
//                BS  E  D4 D5  D6 D7 
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); 
void setup()  
{ 
  lcd.begin(16, 2); 
} 
void loop() 
{ 
  // Display Temperature in C 
  int tempReading = analogRead(tempPin); 
  float tempVolts = tempReading * 5.0 / 1024.0; 
  float tempC = (tempVolts - 0.5) * 100.0; 
  float tempF = tempC * 9.0 / 5.0 + 32.0; 
  //         ----------------  
  lcd.print("Temp         F  "); 
  lcd.setCursor(6, 0); 
  lcd.print(tempF);
    // Display Light on second row 
  int lightReading = analogRead(lightPin); 
  lcd.setCursor(0, 1); 
  //         ---------------- 
  lcd.print("Light           ");  

  lcd.setCursor(6, 1); 
  lcd.print(lightReading); 
  delay(500); 
}

I find it useful to put a comment line above the 'lcd' command.


//                BS  E  D4 D5  D6 D7
 LiquidCrystal lcd(7, 8, 9, 10, 11, 12);



This makes things easier if you decide to change which pins you use.
In the 'loop' function there are now two interesting things going on. Firstly we have to convert the analog from the temperature sensor into an actual temperature, and secondly we have to work out how to display them.
First of all, let's look at calculating the temperature.
  int tempReading = analogRead(tempPin); 
  float tempVolts = tempReading * 5.0 / 1024.0; 
  float tempC = (tempVolts - 0.5) * 100.0; 
  float tempF = tempC * 9.0 / 5.0 + 32.0;
The raw reading from the temperature sensor is first multiplied by 5 and then divided by 1024 to give us the voltage (between 0 and 5) at the 'tempPin' analog input.
To convert the voltage coming from the TMP36 into a temperature in degrees C, you have to subtract 0.5V from the measurement and then multiply by 100.
To convert this into a temperature in Fahrenheit, you then have to multiply it by 9/5 and then add 32.
Displaying changing readings on an LCD display can be tricky. The main problem is that the reading may not always be the same number of digits. So, if the temperature changed from 101.50 to 99.00 then the extra digit from the old reading is in danger of being left on the display.
To avoid this, write the whole line of the LCD each time around the loop.

//         ---------------- 
lcd.print("Temp         F  "); 
lcd.setCursor(6, 0); 
lcd.print(tempF); 

The rather strange comment serves to remind you of the 16 columns of the display. You can then print a string of that length with spaces where the actual reading will go.
To fill in the blanks, set the cursor position for where the reading should appear and then print it.
Exactly the same approach is used for displaying the light level. There are no units for the light level, we just display the raw reading from the analog read. 

Arduino Project Lesson Two; LCD Display Part One

Overview
In this lesson, you will learn how to wire up and use an alphanumeric LCD display. 
learn_arduino_overview.jpg
The display has an LED backlight and can display two rows with up to 16 characters on each row. You can see the rectangles for each character on the display and the pixels that make up each character. The display is just white on blue and is intended for showing text.
In this lesson, we will run the Arduino example program for the LCD library, but in the next lesson, we will get our display to show the temperature and light level, using sensors.

Parts

To build the project described in this lesson, you will need the following parts.

Part

Qty

learn_arduino_lcd.jpg
LCD Display (16x2 characters) 1
learn_arduino_pot.jpg
10 kΩ variable resistor (pot) 1
learn_arduino_breadboard_half_web.jpg
Half-size Breadboard 1
learn_arduino_uno_r3_web.jpg
Arduino Uno R3 1
learn_arduino_jumpers_web.jpg
Jumper wire pack 1

Breadboard Layout

The LCD display needs six Arduino pins, all set to be digital outputs. It also needs 5V and GND connections.
learn_arduino_fritzing.jpg
There are quite a few connections to be made. Lining up the display with the top of the breadboard helps to identify its pins without too much counting, especially if the breadboard has its rows numbered with row 1 as the top row of the board. Do not forget, the long yellow lead that links the slider of the pot to pin 3 of the display. The 'pot' is used to control the contrast of the display.
You may find that your display is supplied without header pins attached to it. If so, follow the instructions in the next section.

Soldering Pins to the Display

The display needs 16 pins, so if your header strip is longer than that then break it off to the right length.
learn_arduino_lcd_kit.jpg
Then put the length of 16 header pins into the solder tabs on the display and starting at one end, solder each of the pins in place. It can be easier to put the long end of the pins into the breadboard so that the header pins are held straight.
If you do not do this, then solder one pin in first and then get the pins in straight, melting the solder on the pin before making any adjustment.
learn_arduino_soldering.jpg

Arduino Code

The Arduino IDE includes an example of using the LCD library which we will use. You can find this on the File menu under Examples → Liquid Crystal → HelloWorld.
This example uses different pins to the ones we use, so find the line of code below:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
and change it to be:

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
Upload the code to your Arduino board and you should see the message 'hello, world' displayed, followed by a number that counts up from zero.
The first thing of note in the sketch is the line:

#include <LiquidCrystal.h>
This tells Arduino that we wish to use the Liquid Crystal library.
Next we have the line that we had to modify. This defines which pins of the Arduino are to be connected to which pins of the display.

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
The arguments to this are as follows:
Display Pin Name Display Pin Number Arduino Pin (in this example) RS 4 7 E 6 8 D4 11 9 D5 12 10 D6 13 11 D7 14 12
After uploading this code, make sure the backlight is lit up, and adjust the potentiometer all the way around until you see the text message
In the 'setup' function, we have two commands:

lcd.begin(16, 2);
lcd.print("hello, world!");
The first tells the Liquid Crystal library how many columns and rows the display has. The second line displays the message that we see on the first line of the screen.
In the 'loop' function, we aso have two commands:

lcd.setCursor(0, 1);
lcd.print(millis()/1000);
The first sets the cursor position (where the next text will appear) to column 0 & row 1. Both column and row numbers start at 0 rather than 1.
The second line displays the number of milliseconds since the Arduino was reset.

Thursday, May 8, 2014

Arduino Project Lesson One:Leds

Overview

In this lesson, you will learn how to change the brightness of an LED by using different values of resistor.
learn_arduino_overview.jpg

Parts

To carry out the experiment described in this lesson, you will need the following parts.

Part

Qty

learn_arduino_red_led_5mm_cropped.jpg
5mm Red LED 1
learn_arduino_R-270-level.jpg
270 Ω Resistor (red, purple, brown stripes) 1
learn_arduino_R-470-level.jpg
470 Ω Resistor (yellow, purple, brown stripes) 1
learn_arduino_R-2k2-level.jpg
2.2 kΩ Resistor (red, red, red stripes) 1
learn_arduino_R-10k-level.jpg
10 kΩ Resistor (brown, black, orange stripes) 1
learn_arduino_breadboard_half.jpg
Half-size Breadboard 1
learn_arduino_uno_r3_web.jpg
Arduino Uno R3 1
learn_arduino_jumpers_web.jpg
Jumper wire pack 1

LEDs

LEDs make great indicator lights. They use very little electricity and they pretty much last forever.

In this lession you will use perhaps the most common of all LEDs a 5mm red LED. 5Mm refers to the diameter of the LED and as well as 5mm, other common sizes are 3mm and the large fun 10mm LEDs.

You cannot directly connect an LED to a battery or voltage source. Firstly, because the LED has a positive and a negative lead and will not light if they are the wrong way around and secondly, an LED must be used with a resistor to limit or 'choke' the amount of current flowing through the LED - otherwise the LED could burn out!
learn_arduino_led_labelled.jpg
If you do not use a resistor with an LED, then it may well be destroyed almost immediately, as too much current will flow through the LED, heating it and destroying the 'junction' where the light is produced.

There are two ways to tell which is the positive lead of the LED and which the negative.
  • Firstly, the positive lead is longer.
  • Secondly, where the negative lead enters the body of the LED, there is a flat edge to the case of the LED.
If you happen to have an LED that has a flat side next to the longer lead, you should assume that the longer lead is positive.

Resistors

As the name suggests, resistors resist the flow of electricity and the higher the value of the resistor, the more it resists and the less electrical current will flow through it. We are going to use this to control how much electricity flows through the LED and therefore how brightly it shines.
learn_arduino_R-270-level.jpg
But first, a bit more about resistors.

The unit of resistance is called the Ohm, which is usually shortened to Ω the Greek letter Omega. Because an Ohm is a low value of resistance (it doesn't resist much at all), we also give the values of resistors in  kΩ (1000 Ω) and MΩ (1000,000 Ω). These are called kilo-ohms and mega-ohms.

In this lesson, we are going to use four different values of resistor, 270Ω, 470Ω, 2.2kΩ and 10kΩ. These resistors all look the same, except that they have different colored stripes on them. These stripes tell you the value of the resistor.

The resistor color code works like this, for resistors like this with three colored stripes and then a gold stripe at one end.

Each color has a number, as follows:
  • Black 0
  • Brown 1
  • Red 2
  • Orange 3
  • Yellow 4
  • Green 5
  • Blue 6
  • Purple 7
  • Gray 8
  • White 9
The first two striped are the first two digits of the value, so red, purple means 2, 7. The next stripe is the number of zeros that need to come after the first two digits, so if the third stripe is brown, as it is in the photograph above, then there will be one zero and so the resistor is 270Ω.

A resistor with stripes brown, black, orange is 10 and three zeros so 10,000 Ω in other words 10 kΩ.

Unlike LEDs, resistors do not have a positive and negative lead. They can be connected either way around.

Breadboard Layout

Connect up your stripboard as shown below, using the 270Ω resistor.
learn_arduino_fritzing.jpg
The Arduino is a convenient source of 5 Volts, that we will use to provide power to the LED and resistor. You do not need to do anything with your Arduino, except plug it into a USB cable.
learn_arduino_overview.jpg
With the 270 Ω resistor in place, the LED should be quite bright. If you swap out the 270 Ω resistor for the 470 Ω resistor, then the LED will appear a little dimmer. With the 2.2kΩ resistor in place the LED should be quite faint. Finally, with the 10 kΩ resistor in place, the LED will be just about visible. Pull the red jumper lead out of the breadboard and touch it into the hole and remove it, so that it acts like a switch. You should just be able to notice the difference.

Turning out the lights might help even more.

Moving the Resistor

At the moment, you have 5V going to one leg of the resistor, the other leg of the resistor going to the positive side of the LED and the other side of the LED going to GND. However, if we moved the resistor so that it came after the LED, as shown below, the LED will still light.
learn_arduino_fritzing_2.jpg
Note, you will probably want to put the 270Ω resistor back in place.

So, it does not matter which side of the LED we put the resistor, as long as it is there somewhere.

Blinking the LED

With a simple modification of the breadboard, we could attach the LED to an output  pin of the Arduino. Move the red jumper wire from the Arduino 5V connector to D13, as shown below:
learn_arduino_fritzing_pin_13.jpg
Now load the 'Blink' example sketch from Lesson 1. You will notice that both the built-in 'L' LED and the external LED should now blink.

Lets try using a different pin of the Arduino – say D7. Move the red jumper lead from pin D13 to pin D7 and modify the following line near the top of the sketch:

int led = 13;

so that it reads:

int led = 7;

Upload the modified sketch to your Arduino board and the LED should still be blinking, but this time using pin D7.