Arduino

Bootloading an Arduino board

I guess by now most of us, coding freaks by now are into arduino directly or indirectly. One major problem we all have faced with arduino is the frequent deletion of the boot-loader from the Atmega IC. There could be N number of reasons for this, and I shall not get into that. I will give you a way to solve this problem and also convert your normal atmega chip to an arduino chip.(Most of you pro’s will tell me AVR and Arduino are the same. YES they are, but for those freshers who have just been introduced to AVR and Arduino take them as different things, as time goes you will know what I mean)Requirements to boot-load:
1) A programmer or another working arduino board.
2)the board you want to boot-load.
3)6-7 plugs(wires)

Procedure:

STEP1:

Connect your programmer to the arduino IC. the pins are as follows:

pin 13,12,11 on the arduino corresponds on sck,miso,mosi respectively. use this config to connect to the usbasp.

STEP2:

On the arduino IDE(arduino1.0 and up) select the corresponding board which you want to boot-load

STEP3:

select your programmer.(usbASP or if your using arduino itself select arduino as ISP)

Step 4:

Connect the programmer to the computer.

Step 5:

In the arduino IDE select tools>burn bootloader

Step 6:

Plug out the arduino and now get coding on your freshly bootloaded chip!!!

Posted by admin in Arduino, 0 comments

Digital Voltmeter

Now that we have established a communication system to the computer which is programming the device, we make use of this feature to make a small digital voltmeter. This is an indispensable tool in the field of electronics!. Whenever there is a need to debug, we need a voltmeter reading to get a better understanding of the circuit. For this example, we’ll use a new tool, Analog Read.So lets get right to it. Onto the theory!

Theory:

The theory here will make an attempt to brief ADC(Analog to digital Conversion). Though most programmable pins are 0 and 5V logic(1 and 0) some pins are also 0 to 5V sensitive. These pins are the analog pins. When a certain voltage is given to these pins, within the limits of the reference voltage of course, a digital equivalent can be generated and read into a register inside the Arduino.

The theory for ‘sampling’ the analog value into a digital value is quite complex, but we will not go into that detail. For, the basic understanding we’ll stick to the picture on the left.

Vref  is the reference voltage to which the input voltage will be digitized. This means that if Vref is 5V, then the input voltage can vary from 0V(common ground) to 5V.
Arduino has a 10 bit ADC. This means that the analog values can be resolved into a digital value ranging from 0 to 255 where 0 corresponds to 0V and 255 corresponds to 5V.

The working is explained as follows:
The input voltage is ‘quantized’ by sampling at constant frequency known as the sampling frequency. This means that every certain amount of time, the value at the analog pin is compared against a set of reference values generated by the main reference voltage.
If vref=5V then, from the circuit on the left, the various reference voltages to the op-amps are 4.375V,3.125V,1.875V,0.625V (got by using resistor divider rule). Therefore, if the Vin(the input voltage) is more than the reference voltage for that op-amp, the output of that op-amp will be high. Lets consider this 2 bit ADC above for a voltage of 2V. 2V<4.375V ,so the first op-amp will send a 5V(logic 1 because its inverting op-amp) as output,similarly its lesser than the second reference(3.125V) hence output is 5V. 2V is>1.875V>0.625V. Therefore the remaining op-amps will give a low output. Now the NAND gates inputs are respectively 1,0 ; 1,1 ; 0,1 ; giving the final output at B as 10 ( 2 in decimal). So, in a 4bit ADC, if the output is 2, the digital reading indicates that the analog value is 3.125V . This quantizing error of 1.25V(max possible error) can be improved by having a higher resolution (i.e higher bit ADC).

The Arduino utilizes a 10bit ADC. This means that 0-5V can be digitized as 0 to 1024 value range. Let’s look at the resolution. 5/1024 =5mV . This means that the most possible error in reading is 5mV! thats more than enough for our usage!.

Enough nerding! lets put it in use! 

Practicals:

The code is very simple. Just a one line add-on to the previous serial code.
In the Loop function, we wish to continuously sample the pin A0 by calling the
analogRead function. This returns a value of 0 to 1024 into the int variable sensorValue.
I leave it to the users to convert this value to a voltage reading.
Hint: in Serial.println , just add a multiplication factor to sensorValue to convert 0 to 1024 to 0 to 5V.

compile and run the code, and in the serial monitor on the computer you should be able to see the voltage value print infinitely amd also change as you rotate the pot.

So simple! 

Food For Thought:

Q.You mentioned that the ADC uses sampling time? Should we take into consideration of the time elapsed?
A. Depends on the application, but most situations does not require it. Do notice that each ADC can only be sampled individually and not together in one shot. So,if you were to sample all analog pins, then time considerations might show up, but even then, its manageable.

Q.Is there a way to change the range of values to an upper limit and lower limit instead of 0 to 1024?
A. yes, It is possible. The function is called map. You can use it like this : int OutputValue=map(sensorValue , 0 , 1023 , 0 , 255) in the above code. This returns a value to the ‘OutputValue’ ranging from 0 to 255. So we have effectively scaled a 10bit to 8bit resolution. This is helpful when data has to be transmitted byte wise in serial communication.

Q.Can analog pins be used as PWM output pins?
A. No,they are strictly read only for analog. But, they can also be used as DigitalWrite or DigitalRead . The parameter to those functions will still be A0-A5.

Q.How do i sample a voltage that is not supplied from the Arduino board(external sensor, etc)?
A.you need to have a common ground between the two devices. Say your external sensor is connected to A0, then make sure that the ground pin of the external sensor is also connected to AGND on the Arduino, which is the in the case of the board ANY GND pin. This way the Arduino knows that the 0V reference of the sensor to calibrate the reading.

Q.How do i sample voltage of different reference(instead of 0-5V , need to use 0-3V)?
A.For this, you need to generate the reference voltage and pass it to AREF pin on the Arduino, I wouldn’t recommend this as it unnecessarily wastes power. Instead use the existing readings and use constrain(click to see usage) to saturate values above 3V. Also note that the AREF voltage cannot be greater than 5.5V.

Posted by admin in Arduino, 0 comments

Serial Communication

The most important part of any project with Arduino I feel is the communication to the user. To display the readings of a sensor or to ask for a command from the user is the basic importance of most projects. Arduino’s most effective form of communication is the Serial Communication. In this tutorial we shall try to understand how to setup a serial communication link with the computer which is programming the device and use the same to debug programs and also to display results in understandable formats.

Theory:

Serial Communication in itself is quite vast. Hence, we’ll address it as different tutorials. This basic tutorial will concentrate on a simplex mode to equip the user to use these tools for future tutorials and the whole communication protocol will be explained in tutorials to come.

Serial communication on the very basic level uses three lines. Tx(Transmitter), Rx(Receiver) and GND(Ground) lines.Data are encrypted as sets of pulses and sent on either the Tx or Rx line with respect to the GND. These pulses are usually 10bit data unless otherwise mentioned. The 10 bits consists of a start bit, a stop bit, and a 8 bit data. This constitutes one packet of data.

The rate at which the bits are transmitted is known as the ‘baud rate’ . Once serial transmission is enabled, the contents of a memory location known as the ‘data buffer‘  is transmitted. As soon as it is empty new data can be written into it and it is transmitted at the baud rate.

This much is enough to understand basic serial transmission. Onto the practical section.

Practicals:

Since this is the basic communication tutorial, we will concentrate on the built in serial communication through the USB cable. The actual way of using serial communication is through the Tx and Rx pins which we will cover in later tutorials.

Components:
– Arduino
– The USB cable that comes along with it to program it.

So this tutorial has no external setup. Onto the code!

The code is the simplest of all!

In the setup we initialize the baud rate of the serial communication.
In this case, its set to 9600. This means that the bits are transmitted at the rate 9600 bits per second.

In the loop we are continuously transmitting the letter ‘a’ infinitely.The subclass println signifies that the Arduino prints the character ‘a’ followed by a ‘ln’ or new line. These again are command bytes sent along with the data. The ASCII equivalent of this would be 10 and 13 (refer to carriage return and new line). This means that the Arduino first prints the character ‘a’ then returns to the start of the line and then creates a new line. These are the sequence of commands sent on the serial line.

When you run this program and look at the Serial Monitor window, you’ll see the character ‘a’ being printed in each new line continuously.If not make sure the baud rate on the serial monitor is set to 9600. If a new line is not desired (if its required to print a string), use Serial.print instead of Serial.println .
Thats it! a simple communication program.

Food For Thought:

A packet of Questions!

Q. Instead of writing ASCII characters, I want to write bytes of hex values. Is it possible?
A. yes, for that its preferable to use Serial.Write(value). It prints the hex values as bytes of data.

Q. I want the data to be transmitted on the Tx Line. Not on the USB..What do i do?
A. It is actually printing on the Tx Line! The USB lines tap the data from these lines and convert it to USB protocols to make life simpler. Otherwise the same data is actually coming on the Tx line as 97(‘a’) 10 and 13.

Q.How important are these baud rates and why the funny number 9600?
A. Well basically, these bauds are standard rates decided long ago for uniformity. Both ends of the Tx and Rx lines need to have the same baud to be able to communicate. If the bauds are different then, the communication wont occur properly. These funny numbers are a result of the dividing possibility of a 11.0592Mhz crystal. That frequency can be divided accurately to get these baud rates. But actually the Arudino uses a 16Mhz crystal  .

Q.What about receiving data on the Arduino?
A. Will be answered in future tutorials as it requires a little more understanding.

Q.How fast can we transmit data ?
A. It depends. Actually after the transmission of a data, its desirable to introduce a delay before transmitting to ensure the buffer is emptied properly. Therefore a delay of 2ms in the above program after the transmission of the character ‘a’ would ensure almost error-free transmission. The baud rate themselves can be increased upto 115200 baud for Arduino family.

Posted by admin in Arduino, 0 comments

Button Interface

Right! so we got an LED blinking on a pin. Cute,but not effective if there is no control over it. Here’s the next part then, to interface a button to the Arduino, to blink the led manually.
Again, the code is very simple. The circuit is also simple, but we’re going to touch upon theory a little more to make it more effective and learn electronics at the same time.
So let’s get started! Onto the Theory!

Theory:

The classic push button is what we’ll learn to interface in this segment. A button is just like a retractable short between two pins. So when you press the button all you are doing is connecting two pins and forming a path for the current to flow between them and if the arduino detects the current it assumes an input has occured. The direction of the current acts as the ‘edge detection’ . We’ll come to this later.
Again, the only parameters we worry about is the voltage and the current. In this case the voltage doesn’t play a role cause the button works at any voltage(its just forming a path!). Its  current dependent in the sense that u can probably give around 2A before the button pin melts :D. Anyways,the Arduino input pin is voltage dependent. Hence we’ll design a voltage based approach as follows.
If we were to put a button between the Arduino and GND , one pin to each, then when we press the button, its equivalent to that pin being directly connected to GND(current flowing towards GND) and when the button is not pressed, its as if the Arduino pin was open(not connected to anything and hence no current flow). This should work great if your code were to be like
If(PIN==GND) {do}

else{ do somethine else}
But, this is not a good way to code because when the pin is open, its like a small antenna which can take in noise and this noise might give erroneous readings. Its best to keep the pin either at high or low. So, the best way would be to do the following

Analyzing this yet again simple circuit, we can notice that the input pin is default powered by the 5V supply through the resistor as long as the button is not pressed(the ends are not shorted to GND). Once the button is pressed, the Input pin is brought to 0V, giving the effect of a ‘falling edge’. This can be recognised as a button press.
Hence we can now write a better code..one when the input pin reads a HIGH and one when it reads a LOW.

Now we are ready to move on to the practical section!

Practicals:

Physical description of most push buttons are as shown. The leads which are NOT nearer are shorted, which means they are the same pin, and the ones nearer are not shorted, which will be the pins that we shall use.

Components Required:
– Arduino Uno
– Push Button
– 10k and 330 resistor
– Red LED

In this example we shall interface a button to the Arduino to toggle an LED.

Yes, the circuit is a little poor in design but the idea and implementation are very simple!. Using the same idea as above in the circuit, we have interfaced the button to Digital Pin 2 of the Arduino keeping the LED at pin 8.
This way the press of a button should be read at pin 2 and effected at pin 8.

We will do two small examples related to push buttons.

The first piece of code we will write is very simple. when the button is pressed, the LED will glow and when it is let go the led goes off.
The code for this is as shown below.Go ahead open up the button code from examples->digital->button

The CODE

Once the code is loaded remember to change the ledPin value to 8 as thats where our led is!
Again here we have the two functions, the setup which is straightforward. It initializes pin 2 as INPUT and pin 8 as OUTPUT. Notice the use of global variables ledPin and buttonPin. These are just to avoid confusion. You can always put the number directly.
In the loop function we see what is called ‘polling’. This refers to reading the input pin at frequent duration and based on the reading, an if else statement effects the output led. The digitalRead function returns the status of the input pin(HIGH  if button unpressed or LOW if button is pressed).

Straightforward right?
Now the problem here is quite obvious. The LED glows only when the switch is held down. Once its let off, the LED turns off too. So, how are we going to fix this?
It might instantly strike that instead of turning the LED on when the switch is pressed, we just toggle the LED that is
int led_status = LOW;
if (buttonState == LOW)
{
if( status==LOW)// if previous state was LOW
{
digitalWrite(ledPin, HIGH);  //output to HIGH state
led_status=HIGH;                    //set status as current state i.e HIGH
}
else                      //if previous state was HIGH
{
digitalWrite(ledPin,LOW);
led_status=LOW;
}
}

This would remember the previous state and toggle the led. This should theoretically work. But when you test this u’ll see a problem. The LED say 3/10 tries might not toggle. This is cause of the ‘switch debouncing’ effect.
This brings us to the second piece of code.. The actual button interface to arduino.

Code 2

When a switch is pressed, there is a change from one state to another(HIGH to LOW or vice versa). Because of the imperfections of the switch, the first contact need not be firm, that is to say when we press the switch, the pin takes time to settle down to the GND value, because the switch toggles from contact to no contact a couple of times before establishing a firm contact between its leads as shown beside in the picture.
This can be wrongly read by the Arduino as a series of press and release of the button, which makes the led blink a couple of times before settling to the appropriate value.
To overcome this we give a delay function so that once we read the pin status we check again after a small delay in the order of milliseconds to see if the change actually occurred or if it was due to debouncing effect.
Slightly modifying the above program we get,

if (buttonState == LOW)
{
    delay(50);    buttonState = digitalRead(buttonPin);
    if(buttonState==LOW)
    {
         if( status==LOW)// if previous state was LOW
{
digitalWrite(ledPin, HIGH);  //output to HIGH state
led_status=HIGH;                    //set status as current state i.e HIGH
}
else                      //if previous state was HIGH
{
digitalWrite(ledPin,LOW);
led_status=LOW;
}
}
}
So what we are doing here is to first see if the button was pressed(i.e shorted to GND). If so, wait for 50mS and again check if it really went LOW. If it did then toggle the LED. else do nothing. This is the perfect way to interface a switch to the Arduino. The final code is as shown below.

Food For Thought:

Here are some Questions:

Q.Why the resistor? why not just directly one pin to 5V and the other to GND?
A. Well!, when you press the switch you will be shorting the 5V directly to GND right? that would damage your power supply. The resistor exists for the sole purpose of limiting the current when the switch is pressed between the power and GND.

Q.Does it mean i can use any value of resistance as long as the power supply can handle the current?
A. Yes you can in theory! but practically a small part of the current will also flow into the Arduino.We do not want that part though a small fraction of the major to affect the Arduino when the switch is not pressed.Also why waste current? keep a low current flowing in the circuit at all times to efficiently use power. But not so low that the Arduino wont even detect it!.

Q.Is debouncing really an issue? it will eventually settle to one state before we realize..
A. It matters when you are using the button to count an event. You wouldn’t want it to count many times when its only once. 

Q.I’m an EC student, we have studied in our curriculum that the pins have an internal pullup resistor to 5V so that we need not use the external resistor.
A. Yes dude, you are right! but theory and practicals are different  . It is a weak internal pullup to 5V. There are always chances the arduino might not read the state right!.

Q.What happens when we digitalRead a pin defined as OUTPUT mode in the setup?
A. I’m not entirely sure on this. But as far as my experimentation goes, it will just return the state of the pin you have written the output pin to. That is, if you have earlier defined the pin in output mode and also written digitalWrite(pin,HIGH) and then follow up with a digitalRead(pin), the function should return the value as HIGH.

Posted by admin in Arduino, 0 comments

Fading LED

In the Last tutorial, we have successfully managed to interface a button to toggle a LED. This time we’ll learn to control the brightness of the LED. We will use something called as AnalogWrite to accomplish this. Ever seen those fading lights on a lot of phones that give a ‘breathing’ effect? Glowing up and dimming down continously? We are going to do just that now! On with the theory!

Theory:

Until now we were using digitalWrite function which is very simple to understand. HIGH referred to 5V or LOGIC 1 and LOW referred to 0V or LOGIC 0. Now we are going to use analogWrite which defines levels between these two voltages and assigns a digital integer number to them. We will try to define a 8 bit DAC(Digital to Analog Converter). To put simply, it just converts a 8 bit value (0 to 255) to voltages ( 0 to 5V). This means we can now access voltage outputs of steps =5/255=0.02V.
We shall touch upon a simple idea of PWM here to get a simple understanding of how this works. Imagine a voltage wave which is on for 0.5 sec and off for 0.5 sec and so on(refer to 50% duty cycle in figure). This means that it alternates between 5V and 0V every one second..if you were to average it out for one second then the average value would be (5+0)/2 = 2.5V . This is the effective DC voltage coming at the output pin.
Now if it was on for a shorter time and off for a longer time(20% duty cycle example), then output = 0.2*5=1.0V.

The effect of these changing voltages will show on the LED as a dimmer source or a brighter source. So effectively all we are doing is to split the 100% into 255 digital levels so that we can have varying voltages of step resolution=0.02V.

So the crux of the theory is this. Suppose we write a value of 128 to analogWrite, its equivalent to a 50 duty cycle of a square wave appearing at the output. Since the LED is a DC device, it will recognise the average value of this wave which is 50% of 5V =2.5V and hence will glow half as bright as it would have for a digital value of 255 =>100% duty cycle => constant 5V.

Now onto the practical section.

Practicals:

This is exactly like the first LED blink program we wrote. The only difference being that the LED is now connected to pin 9 instead of 8. This is something we have to pay careful attention to. The Arduino has only 3 Hardware PWM channels capable of analogWrite( Pins 3,6,9).
The rest are digitalWrite only!.

Nothing more to explain! onto the code!
Go ahead open the code from examples->Basic->Fade.

Yet another easy code!
Setup function initiates pin 9 as an output pin.
In the loop function, we call the function analogWrite to output a 8 bit value to the pin 9. Starting from 0. It is then incremented by a value =fadeAmount preset as 5.
therefore in the successive iterations the value output is 5,10,15,20,etc till 255 once we reach 255 there is an if condition that says fadeamount will now be =-5 which will count down from 255 to 0 and again up to 255 and so on..
once the value is written to the output pin we delay for 30ms . This is purely for the eye to be able to distinguish it rising and dimming.
Pretty simple huh!?
Compile it off and run it to see the magic. I think you can put this code into a night lamp and give a nice effect..of course u’ll need to do the circuit for a lamp not for an LED .. 

Food For Thought:

There always is a Question :-D

Q. Dosen’t the resistor value make a difference for every changing value of the voltage at the pin?
A. No, it does not. The resistor is just a current limiting component. What we need is a changing brightness with respect to the voltage we give. In fact putting a resistor value of 100 ohm when the analog value is approx 70 would give the same brightness as that of 5V and 330 ohm.

Q. What other applications?
A. Quite some actually.. how about a buzzer which can act like a siren. You could also use this to do speed control of motors(future tutorials will show how).

Q.Can this be used as a variable power source from 0V to 5V?
A. Definitely NOT!. Its just a signal not a power source..A power source requires substantial amount of current to support the components its connected to. This cannot act as such a source. But it can trigger a voltage controlled source or a current controlled source.

Posted by admin in Arduino, 0 comments

Hello World

The customary ‘Hello World’ program is the startup code of any computer language, and so it is with electronics too!. But no, we are not printing data here, we make things happen !! (An LED to blink). The simplest is to make an led glow. Many of you may already know this, but i suggest you look into it again as i’m also touching up on the theory part a little. The design of a electronics product not only depends on the aesthetics or the simplification, but also on the efficiency and stability of it. Hence, we shall touch upon the theory of LED too!.

Theory

LED 
A LED is a Light Emitting Diode, a current based device. When sufficient amount of current is passed through this device light is emitted. That said, obvious questions about current,voltage values etc have to be answered. We’ll design all these parameters keeping arduino in mind.
Picture
LED is a diode and sparing the nerdy lectures on diodes.. lets just understand that a diode requires a certain ‘voltage potential’ to break from before current starts flowing through it and that the brightness is dependent on the current.
A typical RED LED to glow would require:
Voltage: 1.8V
Current: 20mA
Going by the data sheet of the AVR ATMEGA328p (the IC on the Arduino UNO), u’ll notice that the max current per output pin is 40mA, which would suggest that we can use one led without any issues. Also,since the arudino is powered by your USB port on the computer, remember that USB can supply atmost 500mA of current. Keeping all these in mind we make some simple calculations.

Assuming that we are using one LED, powering it from a pin and sinking it to the ground, we need to supply 1.8V and20mA at the pin to which it is connected. As the Arduino can only supply a fixed voltage of 3.3V or 5V we need to rework our approach!. We shall use a simple resistor as a current limiting device and to help us with this is OHM’s Law V=IR.

If the pin is sourcing 5V, and we require a current of 20mA, then the resistor value to choose would be 5/20mA =250 ohms choosing the nearest standard resistor value we can use 280 ohms or 330ohms. This should be at its highest brightness.

Now you are ready.. Onto the practical section!

Practicals:

We will use the digital pins of the Arduino to blink an led.
Components required:
                              – Arduino (used UNO here has reference)
– A Red LED(for other colours refer datasheet for the current and calculate!)
– A resistor(330 ohms as calculated earlier)
– A breadboard to house the connections.
LED 
       The positive terminal(anode) of the LED is represented by the longer lead of the LED. Since the Arduino is sourcing the current in this case the longer lead should be connected to the pin of the Arduino used in the programming. For our example we shall use the digital pin 8 for this purpose.
The shorter lead will be connected to the ground via the resistor.

Make the connections as shown below:

Well pretty simple isn’t it!?.. More simple than putting it all up here..hehe..anyways! Onto the coding!
Open up the Arduino IDE
In the examples menu you’ll see under Basics->blink Led program. Load that up. You’ll see the following:

Even to the uninitiated,the code is fairly simple to understand. Anyways i’ll explain the lines.

void setup()
{
pinMode(13, OUTPUT);
}

This function setup is the initializing function. Thats where you put in the initial setup for the various interfaces you are using. In this example, we are just using one pin to glow the LED on and off and hence we initialize that pin to OUTPUT. That completes the setup function.

void loop() {
digitalWrite(13, HIGH);   // set the LED on
delay(1000);              // wait for a second
digitalWrite(13, LOW);    // set the LED off
delay(1000);              // wait for a second
}

he loop function is like the main function, the body of the code. The only difference is that it is like an infinite while loop. Everything inside the loop function is executed infinitely. Again, the body is fairly simple. We are using digital logic (5V -1, 0V -0 ) hence the statement digitalWrite. The syntax is simple : digitalWrite(Pin number,Output_state). When we write it as HIGH, we expect the LED to glow. and when low, the LED will be off.
We introduce a small delay between toggling the LED for reasons obvious to the ‘eye’ . The syntax of delay is delay(time in milliseconds).

Well,pretty simple ain’t it!? This program now blinks an LED on and off at the rate of 1Hz on pin number 13.
Now we’ll need to edit this code slightly to fit our schematics. Remember we have put the LED on pin 8, hence we shall just change two things
1) In the setup function , restate pinMode(13,Output) as pinMode(8,Output).
2)In the loop function , restate digitalWrite(13,HIGH) as digitalWrite(8,HIGH) and digitalWrite(13,LOW) as digitalWrite(8,LOW).

The code will be as follows:

And thats it!!! Compile it, upload it and you’ll see your LED happily blinking on and off. Isn’t it just too easy!? 😉

Food for Thought

Now with obvious questions to the initiated 😉
Q. What happens if I connect the LED between 5V and GND terminal directly?
A. The LED will fuse out. This is because there is nothing to limit the current flowing through the LED except the LED’s own small resistance which is in the order of a few ohms. As earlier, i have mentioned that the board can supply upto 500mA, which means this amount of current finds a easier path through the LED and hence fuses it!

Q. Why should the positive lead of the LED be connected to the pin? why not connect the negative of the LED to the pin and the positive to a 5V supply through a resistor?
A. Yes, this can be done too, but you are advised against it!. When an external voltage source is used, we should check upon that source’s current driving capability. If you were to use the 5V source provided on board, it can safely source 300mA. Now say you forgot to put the resistance. which means that you have just connected the positive lead of the led to 5V and the negative lead to the pin, then 300mA would flow into the pin destroying the pin(as the pin’s max limitation is just 40mA).If careful precautions are taken then connecting this way is fine too.

Q. What if I were to connect the LED in such a way that the positive lead goes into digital pin 8 and negative pin to digital pin 9, and to glow the LED, I set the pin 8 as HIGH and pin 9 as LOW and vice versa to turn it off.
A. An interesting Question!, yes its possible. Notice no resistor has been used, but since the pins are not directly connected to GND or 5V when u program them as 0 or 1 respectively(it consists of a push-pull system), only the required current will flow. Though this is possible, remember that when u turn the LED off you are effectively giving 5V on the negative pin and 0 V on the positive pin. This means that the LED is reverse biased at 5V and it might break down since its break down voltage is around -2V not -5V. Instead of reversing polarity its better to just connect it between pins 8 and 9 and just setting pin 8 as HIGH or LOW that way, the polarities are not reversed, and toggling is also achieved. In this case Pin9 just acts like ground. In this method you’ll notice that the pin’s brightness cannot be controlled.

Q.How do i control the brightness of the LED?
A. Simple, use a different resistor, or in later tutorials, use PWM(Pulse width Modulation) code.

Feel free to ask any other Q’s and I shall add them here for the benefit of everyone. 

Posted by admin in Arduino, 0 comments

Getting Started with Arduino on your GNU/linux machine

Arduino is a micro-controller which is programmable to do many tasks. To program it, a compiler and a linker is required. Arduino has its own IDE named after itself. Following is a guide to install the compiler under Linux(UBUNTU).

Installing Arduino on Ubuntu 12.04

To install Arduino on Ubuntu 12.04 is very simple.
Step 1: Open your terminal.
Step 2: type the following command:
Sudo apt-get install arduino , press ‘y’ if asked to download the dependencies.
Step 3: Arduino is installed, to run just type the command arduino in your terminal or search for the app ‘arduino’ and your good to go.

For using arduino scroll down to the end of the page.

Installing Arduino on Ubuntu 11.10 and previous versions

The latest version of arduino is not present in the ubuntu repositories, so there will be a lil procedure you need to follow.
First you need to install a few dependencies,

sudo apt-get install librxtx-java
sudo apt-get install arduino-core
sudo apt-get install arduino
sudo apt-get install avr-libc
sudo apt-get install gcc-avr

This will install all dependencies and also an older version of arduino on you machine.

Next download the latest version of arduino from here: 32bit/64bit

Once downloaded, extract the files into a location where you want it installed.
go to the location where the files are installed using:

cd /where/you/have/your/files

Now we need to give permissions to the executable files:

sudo chmod 777 arduino

also search for a file with the name avrdude and repeat the same command.

Finally you need to give permissions for the program to use the USB port,so that you do not need to be ROOT always:

for this use the command:

sudo usermod -a -G tty yourUserName

sudo usermod -a -G dialout yourUserName

Log off and log on again for the changes to take effect!

Once this is done you can run the file named arduino.sh to start arduino, if your using the terminal type: ./arduino

Testing your setup!

Connect the arduino board to your computer using the USB cable.
you will notice the power led on the board go ON.

Start the arduino program on your computer, and for an example open the Blink program,
File>examples>basics>blink

Next you need to select the board you are using, If your using a board with atmega8 choose the last option :arduino NG

Then choose the Serial port, in linux the serial port will have the name ttyACM# (# is a number).

once selected, it can be verified at the bottom right of the window.

Then to upload it goto File>Upload.

VOILA! your done.

 

Common Errors!

Though the whole process is simple, you may face errors while working with arduino,the common errors are:

1) Device signature mismatch….<With some technical description>

This means that the atmega chip you are using is either spoilt or creating trouble.
to rectify: bootloader the chip with arduino(will be discussed in future tutorials)
IF it does not work, buy a new atmega, the old one is unusable.

2) some long java exception, but you read permission denied in the first line.

This error occurs when you are running in the user mode and have not granted the proper permissions to all executable.
Browse to the file specified using your terminal and give it appropriate permission using the command:

sudo chmod 777 <file>

3) could not sync sync() error

This occurs if your computer did not find the atmega chip, try disconnecting and reconnecting the board, if this does not work you will have to bootload the chip with arduino again

Posted by admin in Arduino, 0 comments