You are on page 1of 4

Line Follower Session III Arduino

In this and the remaining sessions, we shall mainly focus on microcontrollers and programming on them, Arduino programming in particular. Microcontrollers In the first session, we had described a microcontroller(fondly abbreviated as uC) as a computer embedded on a rather small circuit board. To describe the functions of a uC more precisely, it is a single chip that can perform various calculations and tasks, and send /receive signals from other devices via the available pins. Precisely what tasks and communication with the world it does, is what is governed by what instructions we give to the uC. It is this job of telling the chip what to do, is what we refer to as programming on it. However, the uC by itself, cannot accomplish much; it needs several external inputs: power, for one; a steady clock signal, for another. Also, the job of programming it has to be accomplished by an external circuit. So typically, a uC is used along with a circuit which provides these things to it; this combination is called a microcontroller board. The Arduino Uno that you have recieved, is one such microcontroller board. The actual uC at its heart is the chip called Atmega328. The advantages that Arduino offers over other uC boards are largely in terms of reliability of the circuit hardware as well as the ease of programming and using it. Arduino IDE The Arduino IDE is the software where you can type all your Arduino programs, as well as upload them onto the Arduino board. After you are done typing code, ensure that your board is connected via the USB cable, and simply click on Upload. The program, if found error-free, shall be compiled, written and automatically executed on the Arduino microcontroller. The Hardware Interface Digital Pins The Arduino board communicates with the world via the various pins that have been provided. In particular, there are 13 pins labelled 1 to 13 on one side of the board. These are digital pins, meaning that they can be used for sending/recieving digital signals. The functions digitalWrite() and digitalRead() are used for this. 1. Blink The Uno board has an LED in place on pin 13 by default, which can be used for testing purposes. Navigate to File > Examples > Basic > Blink, to have a look at the code that makes this LED blink. Follow all the function calls that have been made in that program; make sure you understand each of them.

2. Logic blocks Configure pins 7 and 8 in input mode, 13 in output mode. Make pin 13 high only when both the pins 7 and 8 are high. Effectively, you have simulated an AND gate between these three pins. Similarly, simulate other logic functions like OR, NOT. 3. Timing Write a program that keeps pin 13 high as long as pin 7 is high. After pin 7 goes low, pin 13 should remain high for 1 second before going low. Analog Pins 4. Analog Input The pins A0 to A5 on one side of the board are available as analog inputs. The function analogRead() can read the voltage value (between 0 to 5 Volts) at any of these pins. However, this function does not return the actual value of the voltage. Instead, it divides the range of 0 to 5V between 1024 equal parts, and returns an integer value between 0 and 1023, corresponding to whichever part, the read voltage value belongs to. For eg. if input voltage at pin A0 is nearly 0 V, then analogRead(A0) returns 0. If input voltage is almost 5V, it returns 1023. In general, if the returned integer is x, then the input voltage was approximately 5x/1024 in Volts. 5. Analog Output Pins 3,5,6,9,10,11 have a tilde (~) sign before them. These pins can provide an 'analog' output as well. The function analogWrite() is used for this purpose, to set any voltage between 0 to 5V at any of these pins. As in analogRead(), we do not specify the actual voltage to be supplied. Instead, we divide the 0v to 5V range into 256 equal parts labelled 0 to 255, and supply the closest label to the actual voltage value we wish to output. For eg, for 2.5V on pin 7, we use analogWrite(7,128); In general, if voltage v needs to be outputted at pin 8, say, then we should call analogWrite(8,t); where t=256v/5. In reality, the output at these pins is not truely analog. The analogWrite() function only creates the impression of an analog value at these pins. Actually, what happens is that the voltage of the pin is switched very fast between HIGH (i.e. 5V) and LOW (i.e. 0V) levels, and by adjusting the ratios of high time to low time, we can control what value between 0 V to 5 V seems to be present at the pin. This technique is called Pulse Width Modulation(PWM), since the change of output value can be modulated (i.e. altered) by adjusing the width of the HIGH level pulses. Program 1 given at the end illustrates the use of analog read and write functions. i) Write a program that makes pin 13 high as long as the voltage at pin A0 is more that 2V. ii) Write a program that adds the voltages at pins A0, A1 and writes it to pin 9.

Serial.write() and Serial.print() You may be familiar with commands like cout or printf, to display messages on the screen in C. In case of Arduino, the equivalent of these are functions called Serial.write() and Serial.print(). Serial.write() is used to output strings or messages, while Serial.print() is used to output the value of a variable or expression. For eg, Serial.write(Hello World!\n); prints Hello world and goes to the next line, due to the '\n' which is the newline character. Similarly, Serial.print(i+2); will print the value of i+2. Program 2 illustrates both these functions. After uploading the program on the board, open the Serial Monitor. This is where you should see the output of the program. The Serial write and print functions must to be used judiciously to debug your program. Note that you should avoid putting them in a loop where they are likely to be executed forever in quick succession; it will just hang the system! i) Write a program that checks the voltage level at A0 twice every second, and prints the value to the Serial monitor each time on a new line. ii) Modify the above program, to make it print the value only when it is within the range of 1V to 3V Tasks for the week: 1. Start working on the chassis of your bot; get motors and the motor circuit in place. 2. Get your ready sensors circuit for the next session.

Program 1:
void setup(){ //A0 is already in input mode at startup pinMode(10,OUTPUT); } void loop(){ int val=analogRead(A0);//read the value as an integer between 0 and 1023 float in_voltage=val*(5.0/1024);//approx input voltage value. float out_voltage=in_voltage/3;//we shall output 1/3 of the input voltage. analogWrite(10,out_voltage*256/5); }

Program 2:
void setup(){ Serial.begin(9600); /* This statement has to be executed only once; it sets the data transmission rate to 9600 bits per second, as part of a standard protocol called USART. */ } int i=10; void loop(){ if(i>0){ Serial.write(\nThe value of i is );//write is used to print a string Serial.print(i);//print is used to output the value of a variable/expression i--; } }

You might also like