Microcontroller › AVR › interfacing atmega8 with current sensor
- This topic has 9 replies, 4 voices, and was last updated 10 years ago by Ashutosh Bhatt.
-
AuthorPosts
-
August 14, 2014 at 9:51 am #3873gavi prakashParticipant
good afternoon. here i'm doing project on automatic motor control when high load will comes.In this project 1st i'm taking 10 values from current sensor and makng avg and adding ,sub 30%tolarance with that avg.aftre that it will take current values and checking vith that avg +-30%.if it goes high r low motor goes switch off.for that i'm taking PORTC 5th pin as a o/p connecting to relay through relay it will control the motor.it working fine in proteus but coming to h/w it cont giving constant o/p. in hardware part i'm connecting directly o/p of current sensor to controller.can u tell me how to interface current sensor with controller and relay interface with load.can we give o/p of PORTC 5th pin directly to relay?? below i'm sending my code please tell me how to interface in hardware.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay_basic.h>
#define F_CPU 1000000
#define Motor_ON PORTC|=(0X20)
#define Motor_OFF PORTC&~(1<<5))
void delay(unsigned char);
uint16_t ReadADC(uint8_t ch);
void InitADC();
void init_lcd();
void ioinit (void);
void lcd_cmd(unsigned char command);
void lcd_data(unsigned char data);
void adc(unsigned int values);
unsigned int CheckResetButton();
int display(uint8_t t);
uint16_t Current_MIN,Current_MAX,valueofthirtyper,sum,avg;
uint16_t adc_value1,adc_value2,arr[10];
uint8_t t,a,b,c;
void ioinit (void)
{
DDRB = 0b11111111; //PORTB AS O?P
//PORTB = 0b00000000; //Enable internal pullup of pin 27
DDRC=0b00110000; //IN PORT C 4 & 5th pin as a o/p
PORTC|=0b00001000; // switch for taking 10 values
}
void adc(unsigned int values)
{
int h,t,o,ten;
t=(values/1000);
h=(values/100)%10;
ten=(values/10)%10;
o=(values%10);
lcd_data(t+48);
delay(1);
lcd_data(h+48);
delay(1);
lcd_data(ten+48);
delay(1);
lcd_data(o+48);
}
unsigned int CheckResetButton()
{
int i,j;
sum=0;avg=0;
for(i=0;i<10;i++)
{
arr=ReadADC(0);
sum+=arr;
lcd_cmd(0xC0);
adc(sum);
delay(1);
PORTC|=0x10;
delay(2);
PORTC&~(1<<4));
delay(1);
for(j=0;j<20;j++)
delay(2);
}
avg=sum/10;
//return(&avg);
}
void main()
{
Motor_OFF;
int i,value;
//DDRB=0xff;
DDRD=0xff;
//DDRC=0xff;
init_lcd();
InitADC();
ioinit ();
while(1)
{
lcd_cmd(0x80);
value=ReadADC(0);
adc(value);
if (bit_is_clear(PINC, 3))
{
Motor_OFF;
CheckResetButton();
valueofthirtyper=(avg)*0.3;
Current_MIN=(avg)-valueofthirtyper;
Current_MAX=(avg)+valueofthirtyper;
//lcd_cmd(0xC0);
//adc(Current_MIN);
}
if(value>Current_MIN && value<Current_MAX)
Motor_ON; //PORTC 5th pin
else
Motor_OFF;
}
}
void lcd_cmd(unsigned char command)
{
//Put command on the Data Bus
PORTB = command;
//Enable LCD for command writing
PORTD = 0b00010000;
//Allow delay for LCD to read the databus
delay(1);
//Disable LCD again
PORTD = 0b00000000;
//Allow some more delay
delay(1);
}
//Function for sending Data to LCD
void lcd_data(unsigned char data)
{
//Put data on Data Bus
PORTB= data;
//Set R/S (Regiter Select) to High, and Enable to High
PORTD = 0b00110000;
//Allow for delay
delay(1);
//Disable LCD again
PORTD = 0b00100000;
//Allow for some more delay
delay(1);
}
//Function to Initilise LCD
void init_lcd()
{
//Setup both lines of LCD
lcd_cmd(0x38);
//Set Cursor off – Enable LCD
lcd_cmd(0x0E);
//Clear Screen
lcd_cmd(0x01);
//Goto first position
lcd_cmd(0x80);
}
//Delay function
void delay(unsigned char dtime)
{
int i,j;
for(i=0;i<=dtime;i++)
{
for(j=0;j<1000;j++);
}}
//You can use your own delay functions and replace this delay function with your code
/*
*/uint16_t ReadADC(uint8_t ch)
{
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX|=ch;
//Start Single conversion
ADCSRA|=(1<<ADSC);
//Wait for conversion to complete
while(!(ADCSRA & (1<<ADIF)));
//Clear ADIF by writing one to it
ADCSRA|=(1<<ADIF);
return(ADC);
}
void InitADC()
{
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(ADPS1)|(ADPS0);//Enable ADC with Prescalar=Fcpu/128 // For Aref=AVcc;//Wait for conversion to complete
}
thanks in advance
August 14, 2014 at 10:00 am #12016govindrajParticipanthi prakash this is not a possible to get output, please drop this project, thats enough
August 16, 2014 at 3:52 am #12022AJISH ALFREDParticipantHi Govindraj,
I would suggest you to try helping forum members with their technical queries rather than demotivating them.
Hi Gavi,
Please post the kind of current sensor which you are using. Most of the sensors can be directly connected to the analog pins, but there could be exceptions.
Never drive a load directly using the microcontroller output pin. Use a driver circuit according to the load. You can make a driver circuit using a simple transistor or there are driver ICs available like lm2004.
August 16, 2014 at 4:58 am #12024gavi prakashParticipantthank you sir.
i’m using driver circuit at o/p side.o/p of controller is the i/p for transistor.realy is connected to that trnsistor o/p.i’m using load as a cfl bulb.
WCS2720 (current sensor) i’m using.for this can we connect directly to controller???
thanks in advance.
August 16, 2014 at 6:09 am #12025Ashutosh BhattParticipanthow r u rading current values?
what types of values u r gatting?
analog or digital?
August 16, 2014 at 7:10 am #12026gavi prakashParticipanto/p of current sensor analog voltage.i’m converting that to digital by adc and displayng in lcd.please check the above code and tell me how to interface snsor to controller..
August 18, 2014 at 4:43 am #12029AJISH ALFREDParticipantI’ve gone through the datasheet of the sensor and I found that it can be directly connected the way you are doing. Also i found that at 0 current it will give 2.5V output and a per ampere it will make a difference in output voltage of around 65mV only. That means at 0A you will get 2.500V and at 1A you will get 2.565V only.Make sure you’ve connected the reference voltage of the microcontroller to 5V and you are trying to measure very high current like 5A-20A.
August 19, 2014 at 9:02 am #12036gavi prakashParticipantwysiwyg_imageupload:13005:height=30,width=79wysiwyg_imageupload:13006:height=371,width=450
this is the way exactly i connected in hardware part.can you please tell me wether it is right r wrong?
thanks
August 20, 2014 at 7:20 am #12041gavi prakashParticipantAJISH ALFRED, A M BHATTsir c/p of current sensor is 2.5v can i connect directly to controller or tell me which type of current sensor i have to use for better sensitivity.
thanks
August 22, 2014 at 10:15 am #12059Ashutosh BhattParticipantfor better you have to search for sensors with resolution in mA not in A.
like output voltage will change to 50 mV per 10 mA change in current like wise
-
AuthorPosts
- You must be logged in to reply to this topic.