Posts tagged ‘ATMEGA32’

DC Motor Interfacing With Micrcontroller

DC Motors are small, inexpensive and powerful motors used widely in robotics for their small size and high energy out. A typical DC motor operates at speeds that are far too high speed to be useful, and torque that are far too low. Gear reduction is the standard method by which a motor is made useful .Gear’s reduce the speed of motor and increases the torque.

Choosing a DC Motor Depends on application.Prefer following:

  • DCMotor with Gear head
  • Operating voltage 12V
  • Speed

Drive basics of DC Motor

Red wire Black wire Direction of rotation
Positive Negative Clock wise
Negative Positive Anti clock wise
Logic Logic Direction
1 0 Clock
0 1 Anti clock
Direction Pulse to
Clock wise A and C
Anti Clock wise B and D

Bi-Direction control of DC Motor

H-Bridge Circuit using transistors for bidirectional driving of DC motor. H-Bridges in IC’s to reduce the drive circuit complexity . L293D is a dual H-Bridge motor driver, So with one IC we can interface two DC motors which can be controlled in both clockwise and counter clockwise direction and if you have motor with fix direction of motion the you can make use of all the four I/Os to connect up to four DC motors. L293D has output current of 600mA and peak output current of 1.2A per channel. Moreover for protection of circuit from back EMF ouput diodes are included within the IC. The output supply (VCC2) has a wide range from 4.5V to 36V, which has made L293D a best choice for DC motor driver.

As you can see in the circuit, three pins are needed for interfacing a DC motor (A, B, Enable). If you want the o/p to be enabled completely then you can connect Enable to VCC and only 2 pins needed from controller to make the motor work.

**To Move the motor Clockwise And Anticlockwise,Must be use two separate Power source,one for microcontroller and another for driving motor with Driver IC.

Here,i have used ATMEGA32 micrcontroller and  Code is written in C  using AVR Studio 5.0.

Source Code

/*
* DCMotorControl.c
*
* Created: 4/1/2011 12:08:10 AM
*  Author: sfg
*/

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRD=0xFF; //PORTD declared as output
PORTD=0x00;
DDRB=0x00; //PORTB as input
while(1)
{

//Pin 0 of PORTB high,then Moves Clockwise
//A–1–PD0
//B–0–PD1
//Enable–1–PD2
if(PINB==0x01)
{
PORTD=0x05;
//_delay_ms(5000);
}
//Pin 0 of PORTB Low,then Moves AntiClockwise
//A–0–PD0
//B–1–PD1
//Enable–1–PD2
else
{

PORTD=0x06;
//_delay_ms(5000);
}
}
}

Circuit Diagram

Led blinking program with ATMEGA32 and AVR Studio 5.0

The first “Hello World!” project I prefer for Microcontroller is LED Blinking. I have used AVR  microcontroller .  Here,All pins of PORTB declared as output.

Source Code

Here,i have  written code  in C using AVR Studio 5.0.

/*
* ledblinking.c
*
* Created: 3/30/2011 9:36:30 AM
*  Author: sfg
*/

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRB=0xFF; //all pins of PORTB declared as output
PORTB=0x00;
while(1)
{
//TODO:: Please write your application code
PORTB=0xFF;  //High State
_delay_ms(200); //delay
PORTB=0x00; //low state
_delay_ms(200); //delay
}
}

Circuit

Led blinking With Atmega32

Pressure sensor interfacing with Microcontroller

Here , I try to explain is that a pressure sensor detects the pressure and passes an accordingly scaled voltage to microcontroller which converts it into digital data .

For this,i have used ATMEGA32   and MPX4115 pressure sensor. MPX4115 pressure sensor integrates on-chip, bipolar op amp circuitry and thin film resistor networks to provide a high level analog output signal and temperature compensation.More details can be found here MPX4115.

Code is written in C using AVR Studio 4 and winavr gcc compiler.

The ATMEGA32 has an  built-in ADC module. ADC module needs to be initialized.To know  details about ADC initialization and ADC  channel selection,see ATMEGA32 Data sheet.

ADC Initialization

ADMUX=(1<<REFS0);                         // For Aref=AVcc;
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //prescalar div factor =128

ADC Channel Selection

//Select ADC Channel ch must be 0-7

ADMUX=(ADMUX&0xE0)|ch;
//Start Single conversion
ADCSRA|=(1<<ADSC);
//Wait for conversion to complete
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);

Conversion

I have converted  ADC value to pressure using this formula:

adc_result= (5.0/1023.0)*ReadADC(0);

adc_result=((adc_result/5.0)+0.095)/0.009-1;

Source Code

This example code reads analog value from channel 0 and displays it on LCD.

#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include “lcd.h”

void InitADC()
{
ADMUX=(1<<REFS0);                         // For Aref=AVcc;
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //Rrescalar div factor =128
}

uint16_t ReadADC(uint8_t ch)
{
//Select ADC Channel ch must be 0-7

ADMUX=(ADMUX&0xE0)|ch;
//Start Single conversion
ADCSRA|=(1<<ADSC);

//Wait for conversion to complete
while(!(ADCSRA & (1<<ADIF)));

ADCSRA|=(1<<ADIF);

return(ADC);
}

int main(void)
{
float adc_result;
DDRB = 0x00;
//Initialize LCD
InitLCD(LS_BLINK|LS_ULINE);
LCDClear();

//Initialize ADC
InitADC();

LCDWriteString(“Pressure monitor”);
_delay_ms(1000);
while(1)
{
LCDClear();
LCDWriteStringXY(0,1,”Pressure=”)
adc_result= (5.0/1023.0)*ReadADC(0);           // Read Analog value from channel-0

adc_result=((adc_result/5.0)+0.095)/0.009-1;

LCDWriteIntXY(9,1,adc_result,3); //Print the value in 4th column second line

LCDWriteStringXY(12,1,”Kpa”);
_delay_ms(10000);
}
return 1;
}

Circuit Diagram

Pressure sensor interfacing

Pressure sensor interfacing Circuit

Download Source code and circuit diagram