// EDUCATION & TECHNOLOGY: Projects
Showing posts with label Projects. Show all posts
Showing posts with label Projects. Show all posts

Saturday, 4 July 2015

Line Follower Robot using Arduino

This line follower robot is basically designed to follow a black line on a white surface. Any way the same project can be used to follow the opposite configuration with appropriate changes in the software. The entire hardware of this simple line follower robot using arduino can be divided into three parts. The sensor, arduino board and the motor driver circuit. Lets have a look at the sensor first.

Sensor.

The sensor consists of two LED/LDR pairs with appropriate current limiting resistors. The resistance of an LDR is inversely proportional to the intensity of the light falling on it.  The circuit diagram of the sensor is shown in the figure below.
arduino line followerResistors R1 and R2 limits the current through the LEDs. Resistors R6, R8, R3,and R5 forms individual voltage divider networks in conjunction with the corresponding LDRs. When the sensor is correctly aligned, both LED/LDR pairs will over the white surface. In this condition sufficient amount of light gets reflected back to the LDRs and so their resistance will be low. So the voltage dropped across the LDR will be low. When the robot is drifted to  one side , the sensor in the opposite side falls over the black line and the intensity of light reflected back to the corresponding LDR  will be low. As a result the resistance of the LDR shoots up and the voltage dropped across it will be high. The voltages dropped across the right and left LDRs (nodes marked R and L in the above circuit)  are given as input to the analog input pins A4 and A3 of the Arduino board. Right and left sensor outputs observed while testing the above circuit is shown in the table below.
line follower robot using arduino

Arduino uno board.

The arduino board has to be programmed to keep the robot in correct path. This is done by reading the left and right sensor outputs and switching the left and right motors appropriately. Output of the right sensor is connected to the analog input A4 of the arduino and output of the left sensor is connected to the analog input A3 of the arduino. The voltage range that can be applied to a particular analog input of the arduino is 0 to 5V. This range can be converted into a digital value between 0 and 1023 using  analogRead () command.  For example if 3V is applied to A3,  the following code will return 3/(5/1023) which is equal to 613 in the variable leftValue.
int leftInput = A3;
int leftValue=0;
void loop ()
{
leftValue = analogRead (leftInput);
{
From the above table you can see that the voltage across a particular LDR will be 4.4V when it on white and 4.84V when it is on black. The digital equivalent of 4.4V will be 900 and that of 4.84V will be 990 as per the above scheme.  The median of these two values is 945 and it is set as the reference point for the program to check the orientation of the sensor module.
The program identifies the position of the sensor module by comparing the sensor readings with the reference point that is 945. If the reading of a particular sensor is greater than 945 the program can assume that the particular sensor is above black. If the reading of a particular sensor is less than 945 then it is assumed that the particular sensor is above white. If both sensor readings are less than 945 then it means both sensors are on white. If both sensor readings are above 945 it is assumed that both sensors are above black (the same thing happens if we lift the robot off the track). Based on the above four conditions, the program appropriately switches the left and right motors to keep the robot following the black line.

Motor driver.

The motor driver circuit is based on two NPN transistors Q1 and Q2. Each transistors are wired as a switch with a resistor at its base for limiting the base current. The motors are connected to the emitter terminal of the corresponding transistors. A 0.1uF capacitor is connected across each motor  for by-passing the voltage spikes. Back emf  and arcing of brushes are the main reason behind the voltage spikes. If these voltage spikes are not by-passed it may affect the Arduino side.   Circuit diagram of the motor driver is shown in the figure below.
arduino line follower motor driver

Circuit diagram.

Full circuit diagram of the line follower robot  is shown in the figure below.
line-follower

Program.

int leftInput=A3;
int rightInput=A4;
int leftMotor=13;
int rightMotor=12;
int leftValue = 0;
int rightValue = 0;
void setup()
{
  pinMode (leftMotor, OUTPUT);
  pinMode (rightMotor, OUTPUT);
}
void loop()
{
  leftValue = analogRead (leftInput);
  rightValue= analogRead (rightInput);

 if
   ( leftValue < 945 && rightValue < 945)
   {
     digitalWrite (leftMotor, HIGH);
     digitalWrite (rightMotor, HIGH);
   }
   else
   {

     if
     ( leftValue > 945 && rightValue < 945)
    {
      digitalWrite (leftMotor, LOW);
      digitalWrite (rightMotor, HIGH);
    }
 else {
   if (leftValue < 945 && rightValue > 945)
   {
   digitalWrite (rightMotor, LOW);
   digitalWrite (leftMotor, HIGH);
   }
   else
   {
     if (leftValue > 945 && rightValue > 945)
     {digitalWrite (rightMotor, LOW);
       digitalWrite (leftMotor, LOW);
     }}
      }
    }}

Setting up the circuit.

  • First of all remember that each LED and LDR has its own characteristics.
  • Carefully measure the voltage across each LDRs in both scenarios (on white surface and black).
  • A lot of parameters like individual LDR/LED characteristics, ambient light, clearance between sensor and surface etc may affect the result.
  • Get in to your own reference point for the program. In my case it was 945 but you may get a different value.
  • Use a separate power supply unit for powering the motors. Anything above 100mA will be hard for the USB port.
  • The motors used here are 9V/30RPM DC bow motors. If such a configuration is not available, choose the closest one.
  • While soldering up the sensor module, the gap between the two LED/LDR pairs must be selected according to the width of the black line. In my case it was 2cm.
  • Clearance of the sensor from the ground was around 1cm in my case.
  • The sensor LEDs used were 4mm bright green LEDs.
  • The sensor LDRs used were general purpose LDRs.

Simple LED Projects using Arduino

This article is another step forward in learning more about Arduino. We have demonstrated 5 simple led based projects using arduino, which will help you to learn its basic concepts.
1. Blinking Two LED’s using Arduino
As a beginner, if you have tried the “Hello World” program to blink an LED using Arduino; you can try to blink Two LED’s as next project. There are 14 I/O (input/output) pins in your Arduino uno board. These pins are numbered from 0 to 13. They can be configured as either input or output in the sketch you create for arduino. If you have learned the “Hello World” program carefully, you now know that input/output configuration of pins has to be done inside the setup() function. So here is the circuit diagram to blink 2 led’s using arduino.
Blink_2_LED_with_Arduino

Sketch to Blink Two LED’s using Arduino
const int LED1 = 12;
const int LED2 = 13;

void setup()
{
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
}

void loop()
{
  digitalWrite(LED1,HIGH);
  delay(1000);
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,HIGH);
  delay(1000);
  digitalWrite(LED2,LOW);
}

The only difference in this sketch is use of 2 pins in output mode. I have used pin number 12 and 13as output. I have configured them as output inside the setup() function. Inside the loop(), I have written commands to blink LED’s alternatively. When LED1 is ON, LED2 will be OFF. After 1 second LED1 will turn OFF and at the same time LED2 will turn ON. Wait another 1 second and you will see LED2 turning OFF and LED1 turning ON. This cycle repeats.
I have added a photograph of the practical setup I made below.
Blink_2_LEd_with_Arduino
You can watch video of the same circuit below.
2.Control LED with Push Button
If you observe carefully, so far we were just playing with some outputs. We made one LED blink and then we stepped ahead to make two LED’s blink. In both cases we wrote software commands to make our arduino blink led’s automatically at an interval of 1 second. What if we want to control led’s ON and OFF time based on a user input?  This means, I want to give an input manually and based on my input LED should turn ON and OFF. We can use a push button switch to give user input to arduino. In fact, we can use any type of a simple switch like Push to On or Push to Off or a mini push button switch. In this example I am using a “normally open” mini push button switch. A normally open push button switch will be in its open state by default. This switch will close for the time we keep its actuator pressed. If you want to know more about working of different push button switches, you can read our detailed article on push button switches.
http://i2.wp.com/techfeasta.com/wp-content/uploads/2015/04/Control_LED_with_Push_Button.png?resize=693%2C366

I have added the circuit diagram to control LED with arduino using a push button switch.  To connect push button to arduino, we need one of the digital I/O pins configured as a digital input. In this example, I have set pin number 7 as a digital input. So we should connect the push button switch to pin 7 of arduino as shown in circuit. A reference voltage should be connected to one end of switch and the other end of switch should be connected to ground. To avoid a short circuit between pin number 7 and ground, you should connect a resistor (preferable a 10K ohm) in between. The reference voltage is used to detect ON state or closed state of the push button. Arduino board has a readily available +5 voltsreference on power pins cluster. When the push button is pressed, the reference voltage line will get connected to pin number 7.  This voltage will drop across the 10K ohmresistor. So when push button is pressed, a +5 volts is available at pin 7 and this will be considered as state HIGH. On the other hand, when the push button is released (residing in its normally open state), there is no reference voltage line connected at pin 7. On this state, the voltage across 10K resistor is 0 volts (ground potential). This same potential is at pin 7 as well and will be considered as state LOW. This is how ardunio distinguishes between closed (ON) and open (OFF) states of push button switch.
Lets get into the program side of controlling LED using push button switch. In this program, the highlight is instruction to read push button state. APL (Arduino programming language) has an instruction named digitalRead() – which reads a digital input given at the configured input pin. In our program, this instruction reads the status at pin 7 and returns a value according to what it has read. In our example this instruction reads voltage level at pin number 7; returns HIGH if its +5 volts and returns LOW if its 0 volts. Since it returns a value, we have to assign this instruction to a variable while we write the program. We have used the variable val to store the value returned by the instruction digitalRead(). The push button switch is connected to pin 7 and we have assigned this pin 7 to a variable named SW inside our sketch. Inside the setup()function, we have configured this pin 7 (the SW variable) as input using pinMode() instruction. So here is the program.
const int LED = 13;
const int SW = 7;

 int val=0;

void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(SW, INPUT);
  }

 void loop()
 {
   val=digitalRead(SW);
if(val==HIGH)
{
digitalWrite(LED,HIGH);
}
else
{
  digitalWrite(LED,LOW);
 }
 }

3.Toggle LED using Push button
Lets get into next project which is even more interesting. Here we are going to toggle an LED using a push button switch. Toggle means to change state. Our objective here is turn LED ON with first push button press and turn LED OFF with next push button press. This cycle of ON and OFF should continue with each push button press.  The same circuit diagram given above is enough to do this project as we are not manipulating any hardware connection. We just need to change our software (sketch) to change the behavior of this circuit.
Here is the sketch to toggle an LED using push button switch.
const int LED=13;
const int SW=7;
boolean state = true; //declare variable state as boolean
int val=0;

void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(SW,INPUT);
  }
void loop()
{
  val=digitalRead(SW);
  delay(120); // Software debouncing using 120ms delay

if(val==HIGH)

{state=!state; // Compliment variable state using ! operator
  digitalWrite(LED,state);
}
}

I used a “normally open” mini push button switch to implement the circuit. This means a push button press always gives us a “HIGH” state. In other words, we have to sense the closed state of push button switch to turn LED ON and OFF. Turning LED ON and turning LED OFF  both depends on a single event – the press on actuator of push button switch (its closed state – when the voltage at switch = HIGH). We can do this program in many ways. An efficient program always will have less lines of code. In this program, I used boolean instructions and a complement operator. In the program a variable namedstate is declared as boolean and I initialized it as true.  A boolean instruction has only two possible values, either true orfalse. The next highlight of the program is to use of complement operator ( ! ). This is the same negation operator we see in 8051 and other micro controllers. For example, we have an instruction called CPL in 8051 instruction set. This instruction compliments the values in accumulator (0’s with 1 and 1’s with 0’s). In digital electronics, a NOT gate performs the same task.
So here is the working of program. We initialized variable state as true.  Other lines of code are the same we used in previous programs. You already know what is written inside setup(). Lets come toloop(). We sense input of switch withdigitalRead(SW) and store it in an integer variable val. Now we check for the push button press by continuously checking if the variable val has ever registered aHIGH. If it ever registers a high, we compliment the status of state variable and save it to the same variable. Now if the state variable was TRUE before, it has been complimented to FALSE. We write the status of state variable to LED. Based on the value of state variable LED will turn ON and OFF. LED will turn ON if state variable holds a TRUE and LED will turn OFF if state variable holds a FALSE. This process of reading the push button switch and complimenting the state variable continuous.
Note:- In our program, the first push button press actually turns the LED OFF. It will turn ON only with second push button press. From then it will alternate between ON and OFF with each push button press. If you want it to turn ON with first push button press, you just need to make a change in the boolean declaration statement. Declare the variable as FALSE initially.
Note2:- You might have noted a delay(120); instruction just below the val=digitalRead(SW);instruction. It is calledsoftware debouncing technique. This a practical aspect of the circuit. If you are to write the program based on theory, you don’t need a delay instruction here. But there is a practical problem. You may upload the code with out this delay instruction and see the behavior of circuit. Some push button press will actually toggle and some other will not yield an expected result. This behavior is due to 2 reasons. 1) The push button is a mechanical switch. One push button press will yield a series of high pulses (bcz of the vibration created when 2 mechanical parts get in contact) in practice. 2) Arduino is a really fast prototyping platform. In fact its not arduino, its the micro controller used in the board that’s really fast. In an arduino uno, Atmega328 is used which is of 20 MIPS execution capability. This means the controller can execute 20 million instructions per second. It is very very fast than we can imagine. So arduino will sense all these series of high pulses created by one push button press. But we dont need arduino to sense all these pulses. We just need 1 HIGH pulse per push button press. This is a problem created by the bouncing switch and we eliminate this problem through a debouncing technique. There are 2 types of debouncing techniques. Here we apply software debouncing. You may read more about debouncing techniques in our article.

4. Toggle 2 LED using Pushbutton

Our next project is to toggle 2 LED’s using a single push button switch. Here we need one more LED and little tweak in the software. I have added the circuit diagram and program below.  I respect your intelligence. You don’t need an explanation for this circuit and program after learning this much.
Toggle_2_LED_using_Push_Button

I have added the sketch below. Read the sketch carefully. We just need to add a few lines to above program. Its really that simple.
const int LED1=13;
const int LED2=12;
const int SW=7;
boolean state = true; // declare variable state as boolean
int val=0;

void setup()
{
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(SW,INPUT);
 }
void loop()
{
  val=digitalRead(SW);
  delay(100); // Software debouncing

if(val==HIGH)

{state=!state; // Complimenting the status of LED
  digitalWrite(LED1,state);
  digitalWrite(LED2,!state);
}
}
I have added a video of the practically implemented circuit. You can watch it below.
Note 3:- I already wrote that there are different ways to create a program. This same program for toggling LED can be written without using a boolean variable. If we dont use a boolean variable, we can not make use of the compliment operator. This simply means we have to handle the task of switching LED states inside our code. I have added a sketch to toggle an LED without using boolean variable. If you take a closer look, you can see this program is very big and uses more variables and instructions than our previous program (with boolean variable). But both sketches leads the same desired output. So which program is more efficient ? The one with less lines of code!
const int LED1 = 13; 
const int LED2 = 12;
const int SW = 7;
 int flag;
 int val=0;
 int state=1;

void setup() 
{
  pinMode(LED1,OUTPUT); 
  pinMode(LED2,OUTPUT);
  pinMode(SW, INPUT); 
  }

 void loop() 
 {
 val=digitalRead(SW);
 delay(100);
 if(val==HIGH&&state==1)
 {
   digitalWrite(LED1,HIGH);
   digitalWrite(LED2,LOW);
   flag=0;
 }
 if(val==LOW&&flag==0)
 {
 digitalWrite(LED1,HIGH);
   digitalWrite(LED2,LOW);
   state=0;
 }
 if(val==HIGH&&state==0) 
 {
   digitalWrite(LED1,LOW);
   digitalWrite(LED2,HIGH);
   flag=1;
 }
 if(val==LOW&&flag==1)
 {
     digitalWrite(LED1,LOW);
   digitalWrite(LED2,HIGH);
   state=1;
 }
 }

Fan Speed Controlled by Temperature and Arduino

I made this project because I wanted a way to automatically control the speed of a DC fan according to the temperature read by a LM35 sensor. I had a few problems with the PWM part mainly because the fan made a disturbing noise so I had to add a simple RC filter at the output of the PWM pin on the Arduino board.
arduino-temperature-fan-speed-control-550x494

Arduino Sketch
  1. #include <LiquidCrystal.h>
  2. LiquidCrystal lcd(7,6,5,4,3,2);
  3. int tempPin = A1; // the output pin of LM35
  4. int fan = 11; // the pin where fan is
  5. int led = 8; // led pin
  6. int temp;
  7. int tempMin = 30; // the temperature to start the fan
  8. int tempMax = 70; // the maximum temperature when fan is at 100%
  9. int fanSpeed;
  10. int fanLCD;
  11.  
  12. void setup() {
  13. pinMode(fan, OUTPUT);
  14. pinMode(led, OUTPUT);
  15. pinMode(tempPin, INPUT);
  16. lcd.begin(16,2);
  17. }
  18.  
  19. void loop() {
  20. temp = readTemp(); // get the temperature
  21. if(temp < tempMin) { // if temp is lower than minimum temp
  22. fanSpeed = 0; // fan is not spinning
  23. digitalWrite(fan, LOW);
  24. }
  25. if((temp >= tempMin) && (temp <= tempMax)) { // if temperature is higher than minimum temp
  26. fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
  27. fanLCD = map(temp, tempMin, tempMax, 0, 100); // speed of fan to display on LCD
  28. analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
  29. }
  30. if(temp > tempMax) { // if temp is higher than tempMax
  31. digitalWrite(led, HIGH); // turn on led
  32. } else { // else turn of led
  33. digitalWrite(led, LOW);
  34. }
  35. lcd.print(“TEMP: “);
  36. lcd.print(temp); // display the temperature
  37. lcd.print(“C “);
  38. lcd.setCursor(0,1); // move cursor to next line
  39. lcd.print(“FANS: “);
  40. lcd.print(fanLCD); // display the fan speed
  41. lcd.print(“%”);
  42. delay(200);
  43. lcd.clear();
  44. }
  45.  
  46. int readTemp() { // get the temperature and convert it to celsius
  47. temp = analogRead(tempPin);
  48. return temp * 0.48828125;
  49. }
test setup
I used an LCD shield to display the current temperature and speed of the fan, but you can use the circuit without the LCD display. You also need to select the transistor by the type of fan that you use. In my case I used the well-known BD139 transistor and a 9V battery to provide power to the fan and transistor. The LM35 temperature sensor and red led are powered with 5V from the Arduino board.
How does the circuit works?
As you can see in the sketch on the first line I included the LiquidCrystal library (header) that includes useful functions to use when an LCD is connected to the Arduino board. Then I set the pins for the sensor, led and fan.
The most important part is to set the variables tempMin and tempMax with your desired values. tempMin is the temperature at which the fan starts to spin andtempMax is the temperature when the red led lights warning you that the maximum temp was reached. For example if you set tempMin at 30 and tempMax at 35 then the fan will start spinning at 30°C and reach its maximum speed at 35°C.
We store the temperature value in the temp variable and then use some if() functions to check if temp is lower than tempMin and if so let the fan OFF (LOW). The next if() is to check if temperature is higher than the minTemp and lower than the tempMax and if so then use the map() function to re-map the temp value from one value to another. In our case fanSpeed will have a value of 32 at tempMin and 255 at tempMax. These values are used to control the speed of the fan using PWM and the analogWrite().
The fanLCD re-maps the temp to allow the display of fanSpeed in a 0 to 100% range so you can say that the speed of the fan is directly dependent of the LM35′s temperature. When the temperature reaches the value set in tempMax the fan will be at its maximum spinning velocity and the LCD will display FANS: 100% even though the temperature might increase above tempMax.
The rest of the explanation can be read in the comments area of the Arduino sketch.

AVR Studio 4 and 5-Overview for Beginners

if you are reading this you might be interest in our complete tutorial from beginning . if yes Start from : AVR Microcontroller (Atmega32) – An Introduction
For general purpose computers, programs written in one programming language can support different hardware configurations. But things are different for microcontroller.  Each family of micro controllers have its own unique type of hardware architecture, instruction sets, register configuration and memories.
So for each architecture of Micro controller family, there will be at least one compiler available, supporting that particular micro controller family or a couple of other families. Most of them support programming in assembly language while some of them support programs to be written in C language. In rare cases there are compilers that accept Java codes too. But I don’t recommend searching too much for a Java compiler, because most popular Integrated Development Environments (IDEs) don’t support it. AVR studio supports AVR series micro controllers. However the interesting fact is that, even AVR Studio doesn’t support all of the avr series micro controllers. I wrote it is “interesting”  because this IDE has been designed by the manufacturer (Atmel) itself. See release note of each version to know about supported micro controllers or the excluded ones.
So if you are a beginner, I’ll suggest you to begin with AVR studio 4. Later, when you gain experience, start using AVR studio5. They are much different from each other. We will provide a comparative table of their features.

AVR Studio 4

AVR studio 4 provides an integrated development environment(IDE), combined with two other supporting softwares, AVR Toolchain and WinAVR. AVR Toolchain installs the Library for AVR studio. AvrToolchain is a must to run AVR studio. If you install AVR Studio and AVR Toolchain, you’d be able to write program in assembly language. To write a program in C, you need to install WinAVR. If you are thinking to buy a development board from Atmel, it will be an even better decision because AVR Studio supports on board programming. You can write your programs in the AVR studio and you can instantly download & verify it too. Supported programmers are listed below:
  • AVR Dragon
  • AVR One
  • AVR Simulator
  • AVR Simulator 2
  • ICE 200
  • ICE 40
  • ICE 50
  • JTAG ICE
  • JTAG ICE mkll
Programming Languages:                           Assembly Language, C
Software requirements:                               AVR Toolchain 3.0.0, WinAVR 20100110
For supported devices, hardware and software requirement details, see release notes @
Helpful Features:   I/O view pane, multiple document editing in tabs, really good reference manual. Hex file generation.
Total Space Consumption:
  •  AVR Studio 4       307 MB
  • AVR Tool Chain   194 MB
  • Win AVR                 138 MB
  •  Total             639 MB (As calculated by Windows uninstaller)


AVR Studio 5

AVR Studio 5 is much better than that of AVR studio 4. The experience is pretty amazing with AVR Studio 5. It has auto suggestion facility, like, if you start to type a function name, it will look in the library and the C file itself for matches and will list them in a drop down list, from which you can select them. This reduces name conflict. Suppose you want to define a macro or variables or want to introduce a new function, this feature helps very much. This feature is really helpful for a new programmer for whom the library is almost unknown. In addition, using new functions and macros has been made much easier. But to do this, you need the help of Visual studio shell. All the library, compiler and linker are provided with the AVR Studio 5 installer. For rest of the information have look at the release notes.
Programming Languages:             Assembly Language, C
Software requirements: Microsoft visual studio Isolated Shell 2010, AVR Jungo USB, Microsoft .NET framework 4 client profile.For supported programmers, supported devices, hardware and software requirement details See release notes @AVR Studio 5 release notes
Helpful Features:    AVR studio 4 features + single compiler for 8 & 32 bit AVR controllers. In AVR Studio v5 there is no need of installing any additional tool chains. Another useful features of AVR studio 5 are, auto suggestion function, ready to use libraries, readily available examples, integrated online help,  integrated C Compiler,  Atmel AVR Software Framework, Standard APIs, Application Builder, Solution Explorer, In-system programming, JTAGICE3, I/O view etc and much more.
Total Space Consumption:    
  • AVR Studio 5                                          535 MB
  • Visual Studio Isolated Shell             533 MB
  •  .NET framework 4 client profile    182MB
  • AVR Jungo USB                                     2 MB
  • Total                                         1.22 GB (As calculated by windows uninstaller)
For Programming Guide and programming help, see my upcoming posts. For Help about installation, post your comments here.

ISP Programmer for ATmega32 Microcontroller

if you are reading this you might be interest from beginning , if yes start here : ATmega32 Microcontroller introduction
So far I’ve discussed about the micro controller basics and the compiler software. I’ve yet not written anything about programming.
ATmega32 series micro controllers support 3 types of programming
  •   Parallel Programming
  •   ISP Programming or serial Programming
  •   Programming via JTAG
Here ISP stands for In System Programmer. To burn a micro controller just the burning Hardware is not enough, it requires software also that would download the program present in a computer or memory device into the micro controller.
Now the software which I am talking about is named PONY PROGRAMMER 2.06. My circuit is adapted from the website of Pony Programmer. It uses PC COM PORT to download the program into the micro controller. It has a signal amplitude of +5 to +12 volt representing binary ‘1’ and -5 to -12 representing ‘0’.
The micro controller, ATmega32 is programmed using the pins meant for SPI communication. To enable programming, the microcontroller must be taken to the RESET state by pulling its reset pin LOW (Logic 0, or say 0 V). In this state, microcontroller is programmable in either mode (Parallel programming or serial programming). Micro controller always accepts 0 V as logic 0and +5 V as logic one.
The signals transmitted from PC is not in a form that could be accepted directly by the micro controller. Those signals from PC should be made suitable for micro controller. In the programmer, Zener diodes provide necessary conditioning for the signals. It is wired in such a way that it converts ±12 volt signal to +5-0 volt signal which is suitable for the micro controller.  A resistance is necessary to limit the current in the nodes of Zener diodes, without the which the Zener diode may burn off. And computer internal circuits may also receive harms.
A high signal (+5V) in the ‘Reset’ pin of micro controller brings it into operational state. A low signal (Ground) drives it into programming mode. Internal pull up resistors are provided at the reset pins and if nothing is connected to this pin, the micro controller tries to execute the program written within it. Designers can provide a resistance capacitance reset circuit, but it’s not always necessary. Whatever, a push button is used in most cases to provide reset facility. Here in the burner circuit an open collector output is provided to the reset pin and it is driven by the programmer through the port. This much is enough.
The necessary data transmission and reception work portion is handled by the pony programmer. Follow the links provided to download pony programmer. The following burner circuit can burn ATmega32 microcontroller. Connect the derived signals to the adjacent pins, attach the cable to com port, power up the device i.e. micro controller, and the micro controller is ready to be programmed.

ISP Programmer Circuit Diagram

Image:  Circuit diagram of the ISP Burner
ISP burner cable
ISP programmer connection arrangement
So that is how the circuit is set up. Now let me tell you that the micro controller runs upon the internal calibrated RC oscillator in the pictures. So that there is no crystal is attached. Yet the micro controller is programmable. Let us see the software settings. First of all, select the port, to which you have attached the device!  And the programming device from the menu “Set Up>Interface Set Up…”. Select “SI Prog I/O”, this one provides fastest programming speed. And about the ports, it should becom port for this programming cable! Now select the port you have the cable attached to.
Now come to the device selection menu. Here ATmega32 belongs to the AVR family. So it is listed under “Devices> AVR micro”. Select ATmega32 as your device. Auto detection will also do.
Now after this two vital things check out If your circuit is working or not. Choose “Command>Read All”
Now if you see that it is reading the micro controller without any error message, your programming cable is working and you can program the micro controller with it.
You can Burn your hex file with it (Compilers provide hex files to program micro controllers). Just open “File> Open Device File…” now an window will open, now browse for your hex file, load it into the pony programmer, and burn. Pony programmer supports click and drag operation too. That means, if you drop the hex file into the pony programmer, it will automatically catch it.

Components:

  • DB9 female connector
  • 2 X 1K resistors
  • 2 X 5.1 V Zener diode
  • 1 X 15K resistor
  • Vero board
  • BC 547 or any general purpose NPN transistor
  • Male Relimate connector (6PIN & 2PIN)

Links for Reference:

Note: 1. Make sure that you have a COM PORT in your computer hardware and your operating system recognizes it. Check it from “Control Panel > Administrative Tools > Computer Management”. In the cascaded list pane, find the following
“Computer Management > System Tools > Device Manager > Ports (com & LPT)”. If COM ports are present under this list, this circuit will work fine. Else, look for a USB based burner.
Note 2. This burner will work fine for ATmega16 too.  For ATmega8, you need to connect the signals to the respected pins.
Note 3 .You can omit the male 2-PIN Relimate connector

Friday, 22 May 2015

How to control a brushless motor through a ESC with Arduino

Electronic speed control (most commonly known as ESC) are nasty beasts: not from the controlling software point of view but for the way they need to be powered up, and because they need to be calibrated.

Especially the calibration is important because otherwise the ESC doesn’t know which are the limits of the transmitter, and it cannot control precisely the speed of the motor.
Before putting a ESC in any complex Arduino project, it is better to get used to how a ESC works using a very simple sketch.
But before seeing the code, let’s see how to wire it up, because care must be taken otherwise nothing will work.

Wiring up the circuit and powering the ESC

Usually ESCs need a voltage higher than the one provided by the Arduino from his 5V pin: typically they need 2 LiPo cells (around 8V). To achieve that all the circuit must be powered from an external power supply connected directly to the ESC and not via the Arduino, which will be powered by the BEC circuit of the ESC. To make that happen it’s enough to connect the red and black of the control connector to the 5V and GDN of the Arduino board.
The rest of the circuit is pretty easy: from pin 9 of the Arduino we have the signal for the ESC, and into pin 0, the voltage reading from the potentiometer comes in.
Let’s now see the code.

Arduino code

A ESC is made to receive inputs from a R/C transmitter, so it works as a servo: to control it you can use the Arduino Servo Library, so the test code is based on the servo library basic example published on the Arduino site.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <Servo.h>
 
Servo esc;
int throttlePin = 0;
 
void setup()
{
esc.attach(9);
}
 
void loop()
{
int throttle = analogRead(throttlePin);
throttle = map(throttle, 0, 1023, 0, 179);
esc.write(throttle);
}
It just takes the reading of the “throttle”, maps it from 0-1023 to 0-179 (analog reading to servo “degrees”) and then sends it to the ESC via the Servo library. Even in its extreme simplicity this sketch it very useful when you want to calibrate a new ESC to work with the Servo library of Arduino.

Calibration

Now that everything is setup, chances are that you motor might not rotate, or might rotate only forward, or might rotate only to 80% of the throttle (forward and reverse) and then stop: all these are symptoms of lack of calibration, or a calibration done with a different range of PWM signals.
More or less all ESCs have a similar calibration procedure:
  • Power up the ESC while the having maximum forward throttle applied
  • You’ll hear a tone and some beeps and after a while (usually 2 seconds) you’ll hear a confirmation tone and the led will blink a few times with a different color: this indicates that the ESC has measured the wavelenght of max throttle.
  • At this point apply zero throttle (in a fwd/reverse ESC this means full throttle reverse), wait again few seconds for the tones and led to blink: full reverse measured.
  • Then move to central (only for fwd/reverse ESCs) and wait again for the tone and blinks.
  • Have fun with the motors
Another possibility is that even after that procedure, the motor will not turn immediately in reverse, or will only go 50% of forward speed: that’s because ESCs are programmable, and you can change many of their behaviors via either a programming card or via, again as with the calibration, hearing and decoding beeps and blinks.