Monday, September 3, 2012

Introduction to Arduino Part 1

The best way to learn how to make things with Arduino is to study the electricity laws and the details of electronics first.

But I think you can take a shortcut and pass through all this boring stuff and make the exciting first contact with Arduino. Then, if you like the experience, you can study electricity and electronics more profoundly. 

FIRST:  Know the basic components and how it works.

The most basic thing you should know is the Arduino board.

Arduino

Arduino is nothing more than a simple programable computer, and it works like a microcontroller. To make it work, you have to code in the Arduino IDE with a syntax based in Java, and upload the compiled code to the board using the USB connection. And, of course, you need (in most cases) to create a circuit connected to the Arduino board.






This is the Arduino UNO R3


Arduino UNO R3 Parts. Click to enlarge!

1- USB jack
2- power jack,
3- processor
4- communication chip
5- 16 mhz crystal
6- reset button
7- On led
8- TX/NX leds
9- Led
10- Power pins
11- Analog Inputs
12- TX and RX pins
13- Digital Inputs/outputs. The "~" in front of the numbers are for PWM outputs. 
14- Ground and AREF pins.
15-ICSP for Atmega328
16- ICSP for USB interface

The number of pins (Digital in/out and Analog in) vary with the model of the board.  

Power and Connection

The board can work connected to a computer with an USB cable. In that case, the board works with 5V tension and the max current of 50 mA. If the components of the circuit works with that tension and current, you can use only the power that comes from the USB cable. But, if you want the board running alone, you have to connect a external electrical source.

Important: don't fry your Arduino and/or your USB port!!
Never make a circuit that demands a current greater than 50mA in each port. It could fry your Arduino and/or the USB of your computer.

Never change the wiring of the circuit while connected to USB and/or to an external source.

In cases like that (picture below) is pretty easy to make a short circuit if you accidentally touch in something with exposed terminals, like resistors, transistors, diodes. 



So, if you want to make any change in the circuit, is safer to disconnect the Arduino board of USB and turn off the external source.


Power pins

The power pins (number 10 in the picture above) consists of


3.3V: 3.3Volts out
5V: 5 Volts out
GND: 2 Grounds
VIN: The input voltage to the Arduino board when it's using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin.

3.3V pin apply the current of 3.3V.

5V pin apply the current of 5V.

GND is the reference point in an electrical circuit from which other voltages are measured.

VIN apply the voltage and resistance that comes from a external source. For example, if you connect a source of 9V and 300mA, this is the tension and current that you achieve from this pin.

Analog Pins

Analog pins (number 11 in the picture above) are inputs where you connect analogue components like potentiometers and other sensors. While the digital inputs/outputs work only with 0 and 1 values, the analog imputs works with a values from 0 to 1023.

Digital pins


Digital pins 
(number 13 in the picture above) can work as inputs or outputs and you define how it will operate with the function pinMode().
The pins that have the "~" in front of the numbers are PWM (Pulse Width Modulation) out, and they can emulate analog output with analogWrite() function. When you use this function, the board modulates the pulse width, varying the frequency of max and low pulses (0 or 1), emulating an analog pulse.


Thats how analog pulse is emulated with PWM out

The first pulse is a digital PWM and the second is a analog pulse.




Next, the protoboard:

Protoboard

This is a protoboard

"A protoboard, also known as breadboard ( is a construction base for prototyping of electronics. The term is commonly used to refer to solderless breadboard(plugboard).
Because the solderless breadboard does not require soldering, it is reusable. This makes it easy to use for creating temporary prototypes and experimenting with circuit design."(Wikipedia).

And this is how those pins are connected


The pins of the protoboard have a pattern of connection, as shown in the picture above. 

The "a" lines are connected horizontally, but, generally, in the middle of them there are a disconnection. These lines are for power supply, where the power cables are inserted.

The "b" lines are connected vertically. There you insert the components and other cables.



Components

Resistor

Resistors

"A resistor is a passive two-terminal electrical component that implements electrical resistance as a circuit element". (Wikipedia).

resistors symbols

a: Resistor;
b: Variable Resistor (Rheostat);
c: Potentiometer;

This is a Potentiometer

"A potentiometer, informally, a pot, in electronics technology is a component, a three-terminal resistor with a sliding contact that forms an adjustable voltage divider.[1] If only two terminals are used, one end and the wiper, it acts as avariable resistor or rheostat."(Wikipedia).


The colors of the lines indicates the resistivity of the resistor. There is a color code, as you can see below.



 The trick to find the resistance of the resistor is to identify the golden or the silver band, which are the most common index of tolerance. When you find it, this band would be the last of the sequence, and you need to start reading the other way.

 So, for example, if you have this:


The first thing to find is the golden/silver band. In this case, it's golden. So, the sequence is 1-Red, 2-Red, 3-Orange, 4- Gold.
The first band is the first digit, the second band is the second digit. If the third one is the last before the golden band, it is the multiplier, and the last is the tolerance.
So, if you look again at that color code, the reading of the resistor above is 2-2-1kΩ-5%. So, this is a 22kΩ resistor with 5% of tolerance.
More bands = more precision. Generally there are only 4.


Capacitor

Capacitors

"A capacitor (originally known as condenser) is a passive two-terminal electrical component used to store energy in an electric field. (Wikipedia) ".



Capacitors Symbol

Again, there is a color code, as you can see below.


Diode


"In electronics, a diode is a two-terminal electronic component with asymmetric transfer characteristic, with low (ideally zero) resistance to current flow in one direction, and high (ideally infinite) resistance in the other. A semiconductor diode, the most common type today, is a crystalline piece of semiconductor material with a p-n junction connected to two electrical terminals."(Wikipedia).

This is the symbol of diode

In order to know what is the current flow, you need to find the cathode the "negative pole". In the common diode, the cathode is the side with a (white/black) band painted.

LED

LEDs are diodes. LED stands for Light-Emitting Diode. So, there is a polarity too.
In order to know what is the anode e what is the cathode, you have 3 alternatives:

1- The shorter terminal is the cathode (-);
2- The straight side is the cathode (-);
3- Inside the "bulb" the larger leg is the cathode (-);

this is the LED symbol

The oposite of LED is Photodiode.

"A photodiode is a type of photodetector capable of converting light into either current or voltage, depending upon the mode of operation.[1] The common, traditional solar cell used to generate electric solar power is a large area photodiode." (wikipedia).


Photodiodes


Photodiodes can look exactly like LEDs. In order to identify them, the trick is to look for the bulb with larger area.
This is the Photodiode symbol

Transistor

transistors

"transistor is a semiconductor device used to amplify and switch electronic signals and electrical power. It is composed of semiconductor material with at least three terminals for connection to an external circuit. A voltage or current applied to one pair of the transistor's terminals changes the current flowing through another pair of terminals. Because the controlled (output) power can be higher than the controlling (input) power, a transistor can amplify a signal." (Wikipedia).

There are four types of polarity of transistors: NPN, PNP, P-Channel, N-Channel



Usually, transistors have 3 legs: base, emitter and colector. The way you discover which is which is to discover what are the model of the transistor you have. 

For example, below there is a tip122 transitor. You can find the model in the body of the transistor (the letters and numbers highlighted below).

With that number, you use google to search for the data sheet. Use the keywords "tip122 data sheet".
With that, you discover what is the type of the transistor (NPN, in this case) and what is the base, the collector and the emitter.



Relay
Relays

"relay is an electrically operated switch. Many relays use an electromagnet to operate a switching mechanism mechanically, but other operating principles are also used. Relays are used where it is necessary to control a circuit by a low-power signal (with complete electrical isolation between control and controlled circuits), or where several circuits must be controlled by one signal. "(Wikipedia).

Relay Symbol

The relay's switch connections are usually labelled COM, NC and NO:
  • COM = Common, always connect to this, it is the moving part of the switch.
  • NC = Normally Closed, COM is connected to this when the relay coil is off.
  • NO = Normally Open, COM is connected to this when the relay coil is on.
  • Connect to COM and NO if you want the switched circuit to be on when the relay coil is on.
  • Connect to COM and NC if you want the switched circuit to be on when the relay coil is off.


There are mainly two types of relays: a relay with moving parts, and a solid-state relay.

The number of pins vary with the model of relay. To know how your relay operates, google the model of it. For example, the relay below is a "JQC-3F(T73)", so, google the keywords "JQC-3F(T73) data sheet". 



You will discover that this is a 5 pin relay that operates in 12V.
Search again how a 5 pin relay works.
You will find this schematic:



A 5-PIN relay has a single control circuit, but two separate current paths for the switch: One when the relay is de-energized (OFF - no current through the control coil) and the other the energized (ON - current is flowing through the control coil). When the 5-PIN relay is de-energized (OFF), pins 4 and 5 have continuity. When the relay is energized (ON), pins 3 and 5 have continuity.


Solenoid
"Solenoid is a specific type of relay that internally uses an electromechanical solenoid to operate an electrical switch; for example, anautomobile starter solenoid, or a linear solenoid, which is an electromechanical solenoid."(Wikipedia).

The symbol of a solenoid

To know what type of solenoid you need, you have to know the voltage and amperage needed, the current type (Direct Current or Alternating Current), the power (in Watts), the relative period of activity and the course of the rod.
The relative period of activity is measured by this formula: PA=(sum of the active time/sum of the active time + inactive time) X 100%.

For example, the solenoid I have (above) needs 12V, 130mA when it's active, Alternating Current, have 9W, a PA of 100% and a course of the rod of 8mm. 

Drivers: H-Bridge



"An H bridge is an electronic circuit that enables a voltage to be applied across a load in either direction. These circuits are often used in robotics and other applications to allow DC motors to run forwards and backwards. H bridges are available as integrated circuits, or can be built from discrete components." (Wikipedia)


Below, the schematic of how the H-bridge works.






 Discrete H-Bridge
  Discrete H-Bridge

Sensors




Sensors
There are a bunch of sensors. Temperature sensors, light sensor, infrared sensors, distance sensors, gyroscopes, accelerometers...

"sensor (also called detector) is a converter that measures a physical quantity and converts it into a signal which can be read by an observer or by an (today mostly electronic) instrument. For example, a mercury-in-glass thermometer converts the measured temperature into expansion and contraction of a liquid which can be read on a calibrated glass tube. A thermocouple converts temperature to an output voltage which can be read by a voltmeter. For accuracy, most sensors are calibrated against known standards."(Wikipedia).

All the sensors I've ever played with work with analog values (0 to 1023). I'm not sure if all the sensors are analog, but I think most of they are. 
They are connected to the analog input pins of Arduino. They are read by the function analogRead(). 

the most common sensors are:

Temperature Sensor
Distance Sensor

Light sensors


Force Sensors


Accelerometer Sensor


Gyroscope Sensor



Tools
Multimeter


A pretty handy tool you should have is a multimeter. 
"multimeter or a multitester, also known as a VOM (Volt-Ohm meter), is an electronic measuring instrument that combines several measurement functions in one unit. A typical multimeter may include features such as the ability to measure voltagecurrent and resistance. Multimeters may use analog or digital circuitsanalog multimeters (AMM) and digital multimeters (often abbreviated DMM or DVOM.) Analog instruments are usually based on a microammeter whose pointer moves over a scale calibrated for all the different measurements that can be made; digital instruments usually display digits, but may display a bar of a length proportional to the quantity being measured."(Wikipedia).

Instead of teach how to use it, I'll embed a video of a basic tutorial on how to use this thing.




Buying your kit

With all that in mind (you can back here every time you have questions), you can make the next step, that is to buy the Arduino Starter Kit. The basic things you should have is: 
  • Arduino Uno
  • USB cable
  • Breadboard
  • Breadboard cables (8~22 cm). About 70 cables are very handy.
  • A bunch of LEDs, 5 of each is a good number (red / green / yellow/ white)
  • Speaker
  • Piezoelectric Buzzer plate
  • Push button switches
  • Light dependent resistor
  • Adjustable resistor
  • Sensors, (Flame sensor, temperature sensor, Infrared receiver)
  • Resistor kit (200ohm / 1K ohm / 10K ohm) 10 of each is a great beginning
  • 9V battery 
  • Battery slot
  • Storage case 
Testing connection

The next step is to test if everything works.


First of all, download the latest version of Arduino for your OS and install it.


If you have an older Arduino like Duemilanove, Diecimila, or any board with this FTDI chip (image below) you have to install a FTDI driver.

Download the driver for your OS here: http://www.ftdichip.com/Drivers/VCP.htm

These are the drivers you have to install

If your board is newer, like Arduino Uno or Mega 2560, no problem, you don't need to install anything: it'll be detected automatically.

Open the IDE


Connect the Arduino board on your computer.


Go to "Tools"> "Board" and select the model of the board connected to the computer.



Next, go to ""Tools"> "Serial Port" and select the port in which your Arduino is connected.


If your Arduino is plugged, there must be two usb connections and they must be the below the others. For instance, mine is /dev/tty.usbmodem1421

Then, go to "File">"Examples">"01.Basics">"Blink"


Open it, then click on "Upload" (the rounded arrow button), and the program will be compiled and uploaded to the board.


If everything works, the built in led of your Arduino board should blink.


If you selected the wrong board or the wrong port, it will not upload. If it happened, try to select the correct model of your board and the correct port.

Now, you are ready to enter the world of Arduino.

The way I started was following the basic tutorials on Learning session of Arduino website:

I'll not make tutorials since there are good ones already done. I'll teach how to follow the first tutorials, and then you will be by your own. 


Control Flow

It will teach you the bare minimum needed to get started.

The structure of the code is very similar to Processing. 
//------------------------------------------------------------------------------------
before everything, you can declare global variables and import libraries. 

void setup()
{
// With the function void setup(you run the setup code only once. 
}
void loop ()
{
// With the function void loop() you run your main code repeatedly 
}

//------------------------------------------------------------------------------------

The First Circuit
Blink (with led and resistor on the protoboard)

I'll teach how to follow the "hello world" of Arduino, that is a blinking LED.


  • First, you'll notice that there is a description
Blink
This example shows the simplest thing you can do with an Arduino to see physical output: it blinks an LED.
  • What is needed (that is not complete, because the resistor is missing):
Hardware Required
  • Arduino Board
  • LED
  • And the instructions: 
To build the circuit, attach a 220-ohm resistor to pin 13. Then attach the long leg of an LED (the positive leg, called the anode) to the resistor. Attach the short leg (the negative leg, called the cathode) to ground. Then plug your Arduino board into your computer, start the Arduino program, and enter the code below.
Most Arduino boards already have an LED attached to pin 13 on the board itself. If you run this example with no hardware attached, you should see that LED blink.
  • Notice that strange example picture:


It's strange because the resistor is directly connected to the LED
  • And the schematic (that is more precise)
Always follow the schematic in order to know what to do
  • And, finally, the code with a explanation of the functions.

Code

In the program below, the first thing you do is to initialize pin 13 as an output pin with the line
pinMode(13, OUTPUT);
In the main loop, you turn the LED on with the line:
digitalWrite(13, HIGH);
This supplies 5 volts to pin 13. That creates a voltage difference across the pins of the LED, and lights it up. Then you turn it off with the line:
digitalWrite(13, LOW);
That takes pin 13 back to 0 volts, and turns the LED off. In between the on and the off, you want enough time for a person to see the change, so the delay() commands tell the Arduino to do nothing for 1000 milliseconds, or one second. When you use the delay() command, nothing else happens for that amount of time. Once you've understood the basic examples, check out the BlinkWithoutDelay example to learn how to create a delay while doing other things.
Once you've understood this example, check out the DigitalReadSerial example to learn how read a switch connected to the Arduino.
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */


// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {              
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);  
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}







Making the fucking LED blink on and off

In order to make this thing done, just look to the schematic and notice what is really needed:
  • Arduino
  • LED
  • Resistor 220 ohms
And, since that you don't need a soldered circuit of a blinking LED forever, use:
  • Protoboard
  • Protoboard cables
Again: take a peek to the schematic


First: Insert a LED on the protoboard wherever you like, since that you connect it horizontally (the legs of the LED couldn't be at the same column) and notice what is the anode and what is the cathode (the cathode have a short leg)


Next, connect the resistor to the anode of the LED, following the anode column.



Then, connect the cable to the cathode of the LED and to the GND pin of the Arduino


And, finally, connect the cable to the digital pin 13 and to the anode of the LED


Now your circuit is complete. Paste the code below inside the Arduino IDE, connect your Arduino to the computer, and upload it to the board.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */


// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {              
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);  
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}







Your led should blink like this, and your first step is accomplished.



Use the same methodology for all the tutorials of the learning session of Arduino site http://arduino.cc/en/Tutorial/HomePage and you could probably become a self taught master of Arduino.
If you arrived at this point, you have a homework to do, that is to follow all tutorials of that link.

This part of the tutorial is over. Wait for the next.

Tank you for being here!

2 comments:

  1. DataVan Cubee 615 Display in UAE, 10.4-Inches Display in UAE, Second Display in UAE
    https://gccgamers.com/datavan-cubee.html
    DataVan 615 Display in UAE, Safe Shopping Multiple Payment Options Express Delivery GCC Gamers Moneyback Guarantee.
    1633919489165-9

    ReplyDelete