Atmega328p hang issue

Go To Last Post
104 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear All,

 

I am using atmega328p controller to toggle the light. But controller is getting hand after every 2 days. I am not getting any response from the controller. Once i reset the controller it is working fine then.

 

Please look at my program and help with the solution.

 

I am operating two lights with this controller. Program written for three lights. 

 

Here is my program.

 

/*
 * uart atmega328.c
 *
 * Created: 3/23/2019 10:25:20 AM
 * Author : ramkumar.d
 */ 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

/*#define relay1 PORTC0
#define relay2 PORTC1
#define relay3 PORTC2
*/

/*#define exrelay1 
#define exrelay2
#define Buzz*/ 

//Declaration of our functions

void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_putstring(char* StringPtr);
void USART_Flush(void);
char String[]="Hello world!!" , ch , i= 0 , C = 0, I = 0;
unsigned int count = 0 , sec = 0;
int main(void)
{
    DDRC = 0b00000111;
    DDRB = 0B00000000; 
    PORTC |= 0X20;    
    PORTB = 0X0F;
    PORTC |= (1 << 1); // Pin 1 goes low
    PORTC &= ~(1 << 0); // Pin 0 goes low
    PORTC |= (1 << 2); // Pin 2 goes low     
//    PORTC &= 0b00111111;// configuring pin as input
    
    USART_init();        //Call the USART initialization code
    //PORTC5 = 0;
    while(1)         //Infinite loop
    {
        _delay_ms(10);        // delay 10 ms
        
        ch = USART_receive();   // waiting for the character
        
        if((ch == 'R')||(ch == 'r'))   // u/U means boom UP
        {
        
            C = I = 0;
            PORTC &= ~(1 << 0); // Pin n goes high
            PORTC |= (1 << 1); // Pin 1 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("ROK\n");
        }
        else if((ch == 'g')||(ch == 'G')) // d/D means boom DOWN
        {
            C = I = 0;
            PORTC &= ~(1 << 1); // Pin 1 goes high
            PORTC |= (1 << 0); // Pin 0 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("GOK\n");
        }
        else if((ch == 'o')||(ch == 'O'))  //o/O means Optional third relay
        {
            C = I = 0;
            PORTC &= ~(1 << 2); // Pin 2 goes high *optional
             PORTC |= (1 << 0);  // Pin 0 goes low
            PORTC |= (1 << 1);  // Pin 1 goes low
            USART_putstring("oOK\n");
        }
        else if((ch == 'f')||(ch == 'F'))
        {
            C = I = 0;
            PORTC |= (1 << 0); // Pin 0 goes low
            PORTC |= (1 << 1); // Pin 1 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("FOK\n");
        }        
        
        
        count++;
        if(count == 400)
        {
            count = 0;
            USART_putstring("SOK\n");
            if(C)
            {
                USART_putstring("SIOK\n");
                USART_Flush();        // clears the buffer
                C=0;
            }
            USART_Flush(); 
        }
        if(!(PINC & 0b00100000))   // waiting for vehicle to cross the IR sensor
        {
            C = 1;
            //USART_putstring("COK\n");
            USART_Flush();        // clears the buffer
            //_delay_ms(2000);     // delay 2 seconds
            
        }
        USART_Flush();    // clears the buffer

     }
     return 0;
}
void USART_init()
{
    
    UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);  // baud rate register
    UBRR0L = (uint8_t)(BAUD_PRESCALLER);     // baud rate register
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);          // transmitter and receiver enable
    UCSR0C = (3<<UCSZ00) | (1<<UCSZ01);      // frame format: 8data , 2stop bit
}
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return;
    return UDR0;
    
}
void USART_Flush()
{
    unsigned char dummy;
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}
void USART_send( unsigned char data){
    
    while(!(UCSR0A & (1<<UDRE0)));
    UDR0 = data;
    
}
void USART_putstring(char* StringPtr)
{
    while(*StringPtr != 0x00)
    {
        USART_send(*StringPtr);
        StringPtr++;
    }
    
}

 

 

Regards,

Ram.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Comments like:

        if((ch == 'R')||(ch == 'r'))   // u/U means boom UP
       else if((ch == 'g')||(ch == 'G')) // d/D means boom DOWN

do not make this code easy to follow. Presumably this used to be U/D and was later changed to R/G but the comment was not updated?

 

Anyway I don't immediately see anything there that would explain the hang up. Usually something like a hang up that takes 1-2 days to reach would suggest something like a memory leak but there's nothing like that in the code.

 

So is this circuit in an electrically noisy environment (high voltage switching etc)? If so what is done to protect against electrical noise? Even if it isn't do you have a stable PSU, decoupling capacitors and so on?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

ram kumar wrote:
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return;
    return UDR0;
    
}

I don't know how you think you managed to get this to compile

" error: ‘return’ with no value, in function returning non-void"

 

Last Edited: Tue. Aug 13, 2019 - 09:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

 

Three things are happening here

 

1. No response.

2. Garbage data.

3. when i press any letter in the input it is giving all outouts

 

like input ---> 'r'

 

output ---> ROK

                       GOK 
                              oOK

 

                                     FOK

after that if i press reset witch it is working fine

 

 

I am not at all getting what is happening here.

 

Is it a program issue or hardware issue. Actually we have designed our own circuit. 

 

Regards,

Ram.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

MrKendo wrote:

 

ram kumar wrote:
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return;
    return UDR0;
    
}

I don't know how you think you managed to get this to compile

" error: ‘return’ with no value, in function returning non-void"

 

 

It is working fine in my atmel. No errors it is getting. Can you check it again. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Surely the point is that you don't want USART_receive() to return until a character becomes available? As Mr Kendo says if you use:

unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return;
    return UDR0;
    
}

then when you build this you are warned:

		.././main.c: In function 'USART_receive':
D:\test\test\main.c(6,30): warning: 'return' with no value, in function returning non-void
		  while(!(UCSR0A & (1<<RXC0))) return;
		                              ^

But while you could correct this by maybe changing "return;" to be "return 0;" or something would you really want to do this? Back in main() you call this function as:

        ch = USART_receive();   // waiting for the character
        
        if((ch == 'R')||(ch == 'r'))   // u/U means boom UP
        {
        
            C = I = 0;
            PORTC &= ~(1 << 0); // Pin n goes high
            PORTC |= (1 << 1); // Pin 1 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("ROK\n");
        }
        else if((ch == 'g')||(ch == 'G')) // d/D means boom DOWN
        {
            C = I = 0;
            PORTC &= ~(1 << 1); // Pin 1 goes high
            PORTC |= (1 << 0); // Pin 0 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("GOK\n");
        }
        else if((ch == 'o')||(ch == 'O'))  //o/O means Optional third relay
        {
            C = I = 0;
            PORTC &= ~(1 << 2); // Pin 2 goes high *optional
             PORTC |= (1 << 0);  // Pin 0 goes low
            PORTC |= (1 << 1);  // Pin 1 goes low
            USART_putstring("oOK\n");
        }
        else if((ch == 'f')||(ch == 'F'))
        {
            C = I = 0;
            PORTC |= (1 << 0); // Pin 0 goes low
            PORTC |= (1 << 1); // Pin 1 goes low
            PORTC |= (1 << 2); // Pin 2 goes low
            USART_putstring("FOK\n");
        }        
        
        
        count++;
        if(count == 400)
        {
...

So if you do just return while there is no character then it won't match any of the if()/else if() so it will fall straight through to the count++, is that really what you intend? A "normal" USART_receive() looks more like:

unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))); // just wait here until RXC is set
    return UDR0;
    
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:

It is working fine in my atmel. No errors it is getting. Can you check it again. 

OK it's because of different options passed to the compiler.

I was using -std=c99 -pedantic-errors, I think it's the -pedantic-errors which in ths case causes gcc to emit error and not bother trying to go any further and produce any output.

Without it, I get warning like clawson showed above, and it does continue and actually produce an output.

I think you will get returned whatever happens to be in r24 at the time.

But either way, the code is bad!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi clawson,

 

Thanks for your reply.

 

Okay i will changes like this

 

if(ch = USART_receive()!= 0 )

{

 

////code

 

 

}

 

in uart_receive i will put 'return 0'

 

Is it the reason for hang?

 

Actually i am using the same code for two different lights.

 

1st case : AC Lights  

2nd case : DC Lights

 

I am getting issue in the first case only. I do not know why.

 

DC lights are working great. But coming to the AC lights i have to reset the controller for many time to make it alive. 

 

 

 

Regards,

Ram.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MrKendo wrote:

ram kumar wrote:

It is working fine in my atmel. No errors it is getting. Can you check it again. 

OK it's because of different options passed to the compiler.

I was using -std=c99 -pedantic-errors, I think it's the -pedantic-errors which in ths case causes gcc to emit error and not bother trying to go any further and produce any output.

Without it, I get warning like clawson showed above, and it does continue and actually produce an output.

I think you will get returned whatever happens to be in r24 at the time.

But either way, the code is bad!

 

 

 

Thanks for your reply.

 

Can you suggest me what changes i need to do in my code. I am unable to solve the issues.

 

Regards,

Ram.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:
Okay i will changes like this
Why would you do it like that? Do you really need to continue on into the rest of the code if no character is immediately available?

 

Even if you do I'd split it into two functions such as UART_is_char_available() and UART_Receive. Then the calling code would have a structure more like:

while(1) {
    if (UART_is_char_available) {
        ch = UART_receive();
        // handle 'ch'
    }
    do_other_stuff();
}

That will be much clearer to the reader/maintainer of this code as to what is going on. They don't need to know that UART_receive() has a special return value meaning "nothing was available". The UART_is_char_available() function basically just reports back the state of the RXC flag.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Much will depend on the actual build options, but when I build with:

$ avr-gcc -W -Wall -O 1 -g -mmcu=atmega328p foo.c -o foo.elf

... I get:

$ avr-objdump -S foo.elf

.
.
.

unsigned char USART_receive()
{
  while(!(UCSR0A & (1<<RXC0))) return;
  be:	80 91 c0 00 	lds	r24, 0x00C0	; 0x8000c0 <__TEXT_REGION_LENGTH__+0x7e00c0>
  c2:	88 23       	and	r24, r24
  c4:	1c f4       	brge	.+6      	; 0xcc <USART_receive+0xe>
  return UDR0;
  c6:	80 91 c6 00 	lds	r24, 0x00C6	; 0x8000c6 <__TEXT_REGION_LENGTH__+0x7e00c6>
  ca:	08 95       	ret
}
  cc:	08 95       	ret

Note that this almost certainly does not do what you think it does.  It tests UCSR0A once, and if RXC0 is 0, it returns without an explicit return value.  Something >>is<< returned, however.  The register r24 is in fact the return value, so whatever was in UCSR0A is returned.  That is likely to be either 0x20 (if UDRE0 is set) which is an ASCII space, or 0x00, which is ASCII NULL.

 

If RXC0 is set, then the value returned will be that of UDR0, the received byte.

 

In your case, this shouldn't matter, but it still a pretty bad idea.  You cannot control how the compiler will handle a return with no value.  I've only shown one way that it can.  Changing compile options could easily change the correctness of the program.

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:
 count++;
        if(count == 400)
        {

I would change this to:

 count++;
if(count >= 400)
{
    ......

Testing for an "=" here can be missed and will take a long time for count to roll over and possibly be "equal" again, better to look for equal or greater.

I don't think this is your problem, but a good habit to get into. 

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
stack gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:

1st case : AC Lights  

2nd case : DC Lights

 

I am getting issue in the first case only. I do not know why.

Wall warts can be a bane.

If BOD is disabled then mega328P may become anomalous.

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gchapman wrote:

ram kumar wrote:

1st case : AC Lights  

2nd case : DC Lights

 

I am getting issue in the first case only. I do not know why.

Wall warts can be a bane.

If BOD is disabled then mega328P may become anomalous.

 

can you give me more info! that would be great 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

ram kumar wrote:
 count++;
        if(count == 400)
        {

I would change this to:

 count++;
if(count >= 400)
{
    ......

Testing for an "=" here can be missed and will take a long time for count to roll over and possibly be "equal" again, better to look for equal or greater.

I don't think this is your problem, but a good habit to get into. 

 

Jim

 

 

Yes you are correct. I will change this one. Thank you

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


The quality and lifetime of wall warts varies (leakage, noise, transients, instability (oscillates, exceeds tolerance), over-voltage, warranty and/or US PC cycle duration of 3 years, lightning tolerance)

 

re BOD :

via ATmega48A, ATmega48PA, ATmega88A, ATmega88PA, ATmega168A, ATmega1688PA, ATmega328, ATmega328P datasheet

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have few changes as per your suggestions.

 

 

I am getting only single warning.

 

Severity    Code    Description    Project    File    Line
Warning        variable 'dummy' set but not used [-Wunused-but-set-variable]    signal_light_induction    C:\Users\ramkumar.d\Documents\Atmel Studio\7.0\signal_light_induction\signal_light_induction\main.c    154
 

Code

 

/*
 * uart atmega328.c
 *
 * Created: 3/23/2019 10:25:20 AM
 * Author : ramkumar.d
 */ 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

/*#define relay1 PORTC0
#define relay2 PORTC1
#define relay3 PORTC2
*/

/*#define exrelay1 
#define exrelay2
#define Buzz*/ 

//Declaration of our functions

void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_putstring(char* StringPtr);
void USART_Flush(void);
char String[]="Hello world!!" , ch , i= 0 , C = 0, I = 0;
unsigned int count = 0 , sec = 0;
void Rst()
{
    USART_putstring("Rst\n");
    
    wdt_enable(WDTO_15MS);
    //asm("wdr");
    while(1);
}
void disable_wt()
{
    MCUSR = 0;
    wdt_disable();
    return;
}

int main(void)
{
    DDRC = 0b00000111;
    DDRB = 0B00000000; 
    PORTC |= 0X20;    
    PORTB = 0X0F;
    PORTC |= (1 << 1); // Pin 1 goes low
    PORTC &= ~(1 << 0); // Pin 0 goes low
    PORTC |= (1 << 2); // Pin 2 goes low     
//    PORTC &= 0b00111111;// configuring pin as input
    
    USART_init();        //Call the USART initialization code
    disable_wt();       // added
    //PORTC5 = 0;
    while(1)         //Infinite loop
    {
        _delay_ms(10);        // delay 10 ms
        
        ch = USART_receive();   // waiting for the character
        if(ch != 0)
        {
            if((ch == 'R')||(ch == 'r'))   // r--red , g--green , o --- orange, f-- off
            {
                C = I = 0;
                PORTC &= ~(1 << 0); // Pin n goes high
                PORTC |= (1 << 1); // Pin 1 goes low
                PORTC |= (1 << 2); // Pin 2 goes low
                USART_putstring("ROK\n");
                USART_Flush();
            }
            else if((ch == 'g')||(ch == 'G'))             {
                C = I = 0;
                PORTC &= ~(1 << 1); // Pin 1 goes high
                PORTC |= (1 << 0); // Pin 0 goes low
                PORTC |= (1 << 2); // Pin 2 goes low
                USART_putstring("GOK\n");
                USART_Flush();
            }
            else if((ch == 'o')||(ch == 'O'))  
            {
                C = I = 0;
                PORTC &= ~(1 << 2); // Pin 2 goes high *optional
                 PORTC |= (1 << 0);  // Pin 0 goes low
                PORTC |= (1 << 1);  // Pin 1 goes low
                USART_putstring("oOK\n");
                USART_Flush();
            }
            else if((ch == 'f')||(ch == 'F'))
            {
                C = I = 0;
                PORTC |= (1 << 0); // Pin 0 goes low
                PORTC |= (1 << 1); // Pin 1 goes low
                PORTC |= (1 << 2); // Pin 2 goes low
                USART_putstring("FOK\n");
                USART_Flush();
            }
            else if((ch=='t')||(ch=='T'))   // Added  t-- reset
            {
                Rst();
            }    
        }
        count++;
        if(count <= 400)
        {
            count = 0;
            USART_putstring("SOK\n");
            if(C)
            {
                USART_putstring("SIOK\n");
                USART_Flush();        // clears the buffer
                C=0;
            }
            USART_Flush(); 
        }
        if(!(PINC & 0b00100000))   // waiting for vehicle to cross the IR sensor
        {
            C = 1;
            //USART_putstring("COK\n");
            //USART_Flush();        // clears the buffer
            //_delay_ms(2000);     // delay 2 seconds
            
        }
        //USART_Flush();    // clears the buffer
        return 0;

     }
    }
     
void USART_init()
{
    
    UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);  // baud rate register
    UBRR0L = (uint8_t)(BAUD_PRESCALLER);     // baud rate register
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);          // transmitter and receiver enable
    UCSR0C = (3<<UCSZ00) | (1<<UCSZ01);      // frame format: 8data , 2stop bit
}
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return 0;
    return UDR0;
    
}
void USART_Flush()
{
    unsigned char dummy;
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}
void USART_send( unsigned char data){
    
    while(!(UCSR0A & (1<<UDRE0)));
    UDR0 = data;
    
}
void USART_putstring(char* StringPtr)
{
    while(*StringPtr != 0x00)
    {
        USART_send(*StringPtr);
        StringPtr++;
    }
    
}

 

Is it okay now?

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
    if(count <= 400)

Are you sure?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

/*
 * uart atmega328.c
 *
 * Created: 3/23/2019 10:25:20 AM
 * Author : ramkumar.d
 */ 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

/*#define relay1 PORTC0
#define relay2 PORTC1
#define relay3 PORTC2
*/

/*#define exrelay1 
#define exrelay2
#define Buzz*/ 

//Declaration of our functions

void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_putstring(char* StringPtr);
void USART_Flush(void);
char String[]="Hello world!!" , ch , i= 0 , C = 0, I = 0;
unsigned int count = 0 , sec = 0;
void Rst()
{
    USART_putstring("Rst\n");
    
    wdt_enable(WDTO_15MS);
    //asm("wdr");
    while(1);
}
void disable_wt()
{
    MCUSR = 0;
    wdt_disable();
    return;
}
void handle_ch(unsigned char ch)
{
    
    if((ch == 'R')||(ch == 'r'))   // r--red , g--green , o --- orange, f-- off
    {
        C = I = 0;
        PORTC &= ~(1 << 0); // Pin n goes high
        PORTC |= (1 << 1); // Pin 1 goes low
        PORTC |= (1 << 2); // Pin 2 goes low
        USART_putstring("ROK\n");
        USART_Flush();
    }
    else if((ch == 'g')||(ch == 'G'))             {
        C = I = 0;
        PORTC &= ~(1 << 1); // Pin 1 goes high
        PORTC |= (1 << 0); // Pin 0 goes low
        PORTC |= (1 << 2); // Pin 2 goes low
        USART_putstring("GOK\n");
        USART_Flush();
    }
    else if((ch == 'o')||(ch == 'O'))
    {
        C = I = 0;
        PORTC &= ~(1 << 2); // Pin 2 goes high *optional
        PORTC |= (1 << 0);  // Pin 0 goes low
        PORTC |= (1 << 1);  // Pin 1 goes low
        USART_putstring("oOK\n");
        USART_Flush();
    }
    else if((ch == 'f')||(ch == 'F'))
    {
        C = I = 0;
        PORTC |= (1 << 0); // Pin 0 goes low
        PORTC |= (1 << 1); // Pin 1 goes low
        PORTC |= (1 << 2); // Pin 2 goes low
        USART_putstring("FOK\n");
        USART_Flush();
    }
    else if((ch=='t')||(ch=='T'))   // Added  t-- reset
    {
        Rst();
    }

}
void check_induction()
{
    if(!(PINC & 0b00100000))   // waiting for vehicle to cross the IR sensor
    {
        C = 1;
        //USART_putstring("COK\n");
        //USART_Flush();        // clears the buffer
        //_delay_ms(2000);     // delay 2 seconds
        
    }
}
void heart_beat()
{
    USART_putstring("SOK\n");
    if(C)
    {
        USART_putstring("SIOK\n");
        USART_Flush();        // clears the buffer
        C=0;
    }
    USART_Flush();
}
void pin_init()
{
    DDRC = 0b00000111;
    DDRB = 0B00000000;
    PORTC |= 0X20;
    PORTB = 0X0F;
    PORTC |= (1 << 1); // Pin 1 goes low
    PORTC &= ~(1 << 0); // Pin 0 goes low
    PORTC |= (1 << 2); // Pin 2 goes low
    //    PORTC &= 0b00111111;// configuring pin as input
    
}
int main(void)
{
    pin_init();
    USART_init();        //Call the USART initialization code
    disable_wt();       // added
    //PORTC5 = 0;
    while(1)         //Infinite loop
    {
        _delay_ms(10);        // delay 10 ms
        
        ch = USART_receive();   // waiting for the character
        if(ch != 0)
        {
            handle_ch(ch);    
        }
        count++;
        
        if(count <= 10)
        {
            count = 0;
            heart_beat();
        }
        check_induction();  //induction loop
        //USART_Flush();    // clears the buffer
        return 0;

     }
    }
     
void USART_init()
{
    
    UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);  // baud rate register
    UBRR0L = (uint8_t)(BAUD_PRESCALLER);     // baud rate register
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);          // transmitter and receiver enable
    UCSR0C = (3<<UCSZ00) | (1<<UCSZ01);      // frame format: 8data , 2stop bit
}
unsigned char USART_receive()
{
    
    while(!(UCSR0A & (1<<RXC0))) return 0;
    return UDR0;
    
}
void USART_Flush()
{
    unsigned char dummy;
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}
void USART_send( unsigned char data){
    
    while(!(UCSR0A & (1<<UDRE0)));
    UDR0 = data;
    
}
void USART_putstring(char* StringPtr)
{
    while(*StringPtr != 0x00)
    {
        USART_send(*StringPtr);
        StringPtr++;
    }
    
}

joeymorin wrote:

    if(count <= 400)

Are you sure?

 

I am not i have changed it completely now!! 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Added

 

if(count >= 400)
        {
            count = 0;
            heart_beat();
        }

 

 

removed return 0 in while

 

now it is working fine

 

 

Is my code okay now?

 

Last Edited: Wed. Aug 14, 2019 - 02:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:

I am not i have changed it completely now!! 

You've got the wrong comparison.  Should be >= should it not?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Wed. Aug 14, 2019 - 02:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:
Actually we have designed our own circuit. 

you might want to post pictures and schematic of your circuit. I'd guess you have hardware problems as well since there is problems switching the ac lights.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:

ram kumar wrote:

I am not i have changed it completely now!! 

You've got the wrong comparison.  Should be >= should it not?

Yes i have corrected it. Also Return 0 in while.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

ram kumar wrote:
Actually we have designed our own circuit. 

you might want to post pictures and schematic of your circuit. I'd guess you have hardware problems as well since there is problems switching the ac lights.

 

 

I have attached my schematice please take a look at it and let me know your valuable feedback.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can't see any bypass caps on the mega328. The optos to drive the transistors do nothing for your circuit.

 

My guess is transients are getting in via the power supply module.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

I can't see any bypass caps on the mega328. The optos to drive the transistors do nothing for your circuit.

 

My guess is transients are getting in via the power supply module.

 

Okay, One ore doubt i have, Will the unused pin causes the controller to hang?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Will the unused pin causes the controller to hang?

Only if you try to test their logic state.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not too bad, though why have you xtal floating around in some random spot---it should be shown connected right to the processor.  

 

The when someone looks at the processor, they can see you are using a crystal right away, not when they happen to see it later on page 5 of a schematic.

 

 

1K seems a bit high for the opto leds (especially at 3.3V drive)....might want to double check (I did not)...They'll only get a bit more than 1 ma.

 

How does power get from U2 (reg) to U3  (AVR).?.I don't see a path  Showing clear connection wires will eliminate having to hunt & guess.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

How does power get from U2 (reg) to U3  (AVR).?.I don't see a path  Showing clear connection wires will eliminate having to hunt & guess.

 

iN con10 we will short it to 3.3v

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What may can resolve this hang issue? Connecting 0.1uf at vcc and ground!!! Will it resolve?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

ram kumar wrote:
iN con10 we will short it to 3.3v

Is 3.3v and 16MHz xtal in spec for M328P......

I don't think so!

If I'm reading that correctly, you need a VCC of at least 3.78v for 16MHz to get it in the safe zone.

Jim

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
stack gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Wed. Aug 14, 2019 - 12:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are there no part numbers for the optocouplers?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

Why are there no part numbers for the optocouplers?

 

Part number is : Pc817

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Guys,

 

our circuit is also working with 5V, we have an option. 

Please Suggest, as per the Speed grade , Can we go with 5V as VCC.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atmega328p drives  Pin low (with very Low sink current, so that no loading Effect on Micro controller) ,then opto coupler working and Relay is ON.

 

But  Why this is hang while running. Please suggest your valuable Answers.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you guys working on the same project? Or is this a hijack by Srinivas?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Srinivas_nipun wrote:
But  Why this is hang while running. Please suggest your valuable Answers.

Well an AVR does not hang, but it can be caught in a loop with no exit condition, so look at your code and see where you have a while() loop that its exit condition is not being met.

Most likely in one of your USART functions, so start there, and see which one is not exiting its loop.

 

Yes, you can run at 16MHz with a vcc of 5v.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
stack gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:
frame format: 8data , 2stop bit

Why 2 stop bits, that is unusual these days, was common back in the teleprinter days at 110 baud?

Note the default for this register is 8 bit, no parity, and 1 stop bit, no need to change this register if using this common format!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
stack gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Are you guys working on the same project? Or is this a hijack by Srinivas?
IP addresses suggest they are 4,300km apart so it suggests that Srinivas is not in any way related to ram kumar but with things like VPN these days one never knows.

 

(Having said that I spend a lot of my life working with colleagues who are 5,000 miles away so in this day and age physical location maybe doesn't mean so much? You can have collegaues working on the same project on opposite sides of the globe (if you can handle the time difference!))

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

Are you guys working on the same project? Or is this a hijack by Srinivas?

 

We are a team. I works on  programming. he works on hardware.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

Srinivas_nipun wrote:
But  Why this is hang while running. Please suggest your valuable Answers.

Well an AVR does not hang, but it can be caught in a loop with no exit condition, so look at your code and see where you have a while() loop that its exit condition is not being met.

Most likely in one of your USART functions, so start there, and see which one is not exiting its loop.

 

Yes, you can run at 16MHz with a vcc of 5v.

 

Jim

 

 

But it is working great with DC lights. What is the difference switching between AC lights and DC lights. Same code same circuit but AVR hanging with only AC switching.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I still think that 5 second cycle strongly suggests watchdog. Are you unable to use a debugger?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did i not suggest earlier that the power supply module might be part of the problem as well as poor circuit layout? Current flows in a loop - but probably not where you expect it. Understand where it flows and you might find the solution.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

I still think that 5 second cycle strongly suggests watchdog. Are you unable to use a debugger?

 

Jim

 

Yes, I will change that one. Actually we are facing this problem from the last 2 to 3 months. I have added this WDT just after i posted this thread.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Did i not suggest earlier that the power supply module might be part of the problem as well as poor circuit layout? Current flows in a loop - but probably not where you expect it. Understand where it flows and you might find the solution.

 

Yeah you suggested. We are discussing about this also. I am very confusing in this switching too. Because the same device, same controller, Same power supply only the difference is light type(AC and DC)!!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If we can’t see your layout, how can we suggest a fix?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Severity    Code    Description    Project    File    Line
Warning        variable 'dummy' set but not used [-Wunused-but-set-variable]  

 

What is this warning for. 
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:

Severity    Code    Description    Project    File    Line
Warning        variable 'dummy' set but not used [-Wunused-but-set-variable]  

 

What is this warning for. 

It should be clear, no?  Your variable 'dummy' is set, but it is never referenced by other code.

 

 void USART_Flush()
{
    unsigned char dummy;
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}

That is, you write it, but never read it.

 

You can fix this in a few ways.  One way is to use a variable attribute:

unused

This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC does not produce a warning for this variable.

So:

 void USART_Flush()
{
    unsigned char dummy __attribute__ ((__unused__));
    while ( UCSR0A & (1<<RXC0) ) dummy = UDR0;
}

 

Or, you can omit the variable altogether:

 void USART_Flush()
{
    while ( UCSR0A & (1<<RXC0) ) UDR0;
}

Since UDR0 is volatile, the compiler will still reference it as required.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is used to alert you that you have never used a value that is set. For example:

 

dummy = foo(MyVariable);

And then never using "dummy" for anything. Remember that, in C, you DO NOT have to assign a variable to a value returned from a function. For example, you can  just do:

 

foo(MyVariable);

As I have found, somewhat painfully, assigning a variable to a return value, and then never using it, except for checking it with the debugger, is a waste because the compiler will never use, or perhaps even create, that variable, anyway.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
If we can’t see your layout, how can we suggest a fix?

 

I will update tomorrow morning. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi  Kartman ,

 

find the Attachments for Layout files.(PDF)

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks like there was no account for creepage on this board.With mains voltage you need clearance around the tracks that carry it - especially around the relays. Start with 6mm. This is required for safety.

 

Why the copper fills on both sides of the pcb? You really only need one on the solder side.

 

No bypass caps on the mega328. The pcb layout is less than optimal - I'd suggest (for your safety) you scrap this board and carefully re-lay a new one. I'd be putting the realys at the end of the pcb rather than in the middle.

 

Last Edited: Fri. Aug 16, 2019 - 12:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Kartman,

 

I have used Opto-couplers in My Design, As we know this Opto couler Isolate the Input stage(MCU) and out Put Stage(Relay Section).So that Whatever things happens in the Out put doesn't reflect On the input stage. we can use Either AC related or DC related Application.

 

As usual, for proper grounding, used copper on Both sides,  this is 2 layer PCB but if we go with for 4 layers, copper must be used on both sides. I think copper pouring is not the proper reason for hang the controller while running.

 

Because,

 

same PCB is running well in another Application with Another Code, no hanging issues there, but why this happens here, we don't know.

 

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your opto couplers do nothing in your design - why? The ground is shared, so there is absolutely no advantage. Relays provide the galvanic isolation anyways.

 

 

Whatever things happens in the Out put doesn't reflect On the input stage.

You hope. Unfortunately the real world has capacitance. Because your copper pour goes close to the relay contacts, you've got plenty of coupling for transients to get into your circuit. It is also illegal is most western countries and unsafe if the relays are switching mains power. Accept that your pcb layout is bad - very bad in fact and learn from it. Have a look at the circuit board for a modern washing machine ,dryer or dishwasher and see how they do it. They're cost driven, so they don't do anything they don't have to. There's also product liability, so they'll follow the required standards. So, if they put slots in the pcb or do anything that adds cost - it's for a good reason.

 

The copper pours on both sides are useless without proper stitching between the two layers. For your application, copper pour on one side of the pcb should be more than adequate. 

 

 I think copper pouring is not the proper reason for hang the controller while running

How does the other application differ? How have you proven this? On one hand you're asking for assistance to solve your problem, but on the other you're very sure of what isn't the problem. Rule out the obvious problems and you're left with what isn't obvious.

 

From what we've been told so far, the problem happens when the AC lamp is switched. Here's some suggestions as to what I'd probably do if faced with the same problem:

 

1. Address the obvious problems - no bypass cap close to the mega328. Ensure the BOD is enabled.

2. Have a flashing led so you can easily see when the controller has crashed.

3. switch the AC lamp at a regular interval

4. run the above test. Does the unit crash or misbehave? Repeat the test a number of times to see if you can repeat the problem.

5. if it does fail, then I'd remove the relay that switches the AC (or wire a separate one) from the pcb and run wires. Ensure the AC wiring is protected so you don't electrocute yourself.

6. run test again. Any difference? This would prove/disprove my assertion.

 

For the test, make sure you don't have any other wiring (apart from the mains input) to the board. Any mains borne transients will usually find their way back via external wiring.

 

 

Other things to consider:

 

Any wiring from external sources needs to be protected. There's mention of a loop detector input - how is that protected from ESD,EMI etc? How does your software filter this input? What is considered a valid input? Is it a 1us pulse or maybe a pulse at least 300ms wide? How would your code determine the difference?

 

Layout of the wiring in your system - don't run high current and/or mains wiring close to your signal wiring. Ensure there is adequate separation. 

 

 

 

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

 

Any wiring from external sources needs to be protected. There's mention of a loop detector input - how is that protected from ESD,EMI etc? How does your software filter this input? What is considered a valid input? Is it a 1us pulse or maybe a pulse at least 300ms wide? How would your code determine the difference?

 

Layout of the wiring in your system - don't run high current and/or mains wiring close to your signal wiring. Ensure there is adequate separation. 

 

There is an induction loop which tells us when any metal detected. There is a relay in that induction loop. Relay gets high and output will short when any metal detects. So in the output i have connected my I/O pin and ground. 

 

When any metal detects I/O pin and GND will get short that's it. Is it fine right?

 

My controller will get the power through UPS.

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nowhere near enough information. How long is the wiring between the relay and the input? Understand that every bit of wire is an antenna - it can radiate and receive. It is also a point where ESD can enter.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
How long is the wiring between the relay and the input?

 

It is about 60cm. Can i know how to avoid this ESD and EMI?

Last Edited: Tue. Aug 20, 2019 - 06:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:
It is about 60cm

60cm...antenna.

 

But anyway, I saw the board, the layout is very bad....just accept it as kartman said, redraw eveything from scratch, and please take a look at the Good PCB design suggestion...there is plenty on the internet. forgetting the decoupling capacitors suggests that this is your first ever PCB ? then at this point you should stay away from AC stuff and just consider playing around with 1.8 - 12V DC sources...other than this I think the guys here gave their best, you cant just keep arguing for a week, asking questions and not accepting any answer or willing to do any suggestion. think again, re-draw the board from the scratch and only then you could get a valid suggestion.

 

other than this, this is just a waste of time.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ram kumar wrote:
Can i know how to avoid this ESD and EMI?

 

As I said, its better to go read a little bit about hardware design, good PCB practices...etc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Moe123 wrote:

ram kumar wrote:
It is about 60cm

60cm...antenna.

 

But anyway, I saw the board, the layout is very bad....just accept it as kartman said, redraw eveything from scratch, and please take a look at the Good PCB design suggestion...there is plenty on the internet. forgetting the decoupling capacitors suggests that this is your first ever PCB ? then at this point you should stay away from AC stuff and just consider playing around with 1.8 - 12V DC sources...other than this I think the guys here gave their best, you cant just keep arguing for a week, asking questions and not accepting any answer or willing to do any suggestion. think again, re-draw the board from the scratch and only then you could get a valid suggestion.

 

other than this, this is just a waste of time.

 

Okay we will come back with new design. Thank you for valuable suggestions. 

 

Moe123 wrote:
As I said, its better to go read a little bit about hardware design, good PCB practices...etc.

 

Yes correct. We will work on it.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Kartman,

 

Modifications done as per your's suggestions.

 

please comment on this.

 

By pass caps added and Copper removed near by Relays.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The opto’s won’t do much will they since you have no galvanic isolation. Yes,it was fun reverse engineering the schematic from the pcb.
What are your clearance distances?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

thanks Kartman,

 

Line to Line ,thru pin, smd pin, thru via ...... 10mils

 

Line to Shape ...... 20 mils

 

and 

 

How can I modify to get Galvanic Isolation.

 

 

           

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Srinivas_nipun wrote:
How can I modify to get Galvanic Isolation.

 

Please take a look here at this presentation:

http://pwrsocevents.com/wp-conte...

 

and this article:

 

https://www.allaboutcircuits.com...

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks  Moe123 ,

 

so that need to separate signal ground(opto coupler input) and  relay ground(opto-coupler out ) to get the Galvonic Isolation.

 

 I am getting 5V from AC supply and converted into 3.3V, this 3.3V used for opto coopler input and Micro controller.5V for Relay with common Ground for Both 5V and 3.3V.

 

    How can I modify to separate Grounds.

 

Also,

 

Can I remove the Opto Coupler and Atmega328P input can Drive the Relay.(both Atmega and Relay at 5V).

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

here is My Schematics Diagram.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

is it just me who thinks that the schematic is inconsistent with the board or am I missing something ?

 

I suppose the ATmega_Power is the going to Vcc, you have 2 caps with 100nF, the datasheet suggests only one cap 100nF Vcc <-->GND and as close as possible to the MCU, I dont see in the Board  C8 and C9. Does Eagle ignore the missing components when doing (what i suppose you did) a forward annotation, 

 

the Atmega_Power global label, where does it go ?? I dont think you have reworked your board as we suggested, you messed it up again.

 

Please confirm that this schematic is related to your board the one that you posted! its very dangerous to change something in the board without changing your schematic.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

to the freaks who used Eagle: does eagle ignore such errors like missing components in the board but they are still in the schematic ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks  Moe123 ,

you can find C8 on the silk screen bottom(ssb), Pin No 7 is the VCC and Pin No:8 is GND, I placed C8 Between 7&8. " Atmega_Power global label "  Modified as VCC

 

My tool is Allegro 16.6.

 

CN9 is used for power option to MCU, either 3.3V or 5V.

 

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you confirm that you made your board according to your schematic....same labels...same everything ?? or you were just deleting here and there and then adding random components ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes, whatever in the Schematics is on the Board.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


- AREF is left floating, decouple it to 100nF.

- the other Vcc should also be decoupled to 100nF... (sorry but its very difficult to read your schematic...incosistency is everywhere).

 

Page 6 via

http://ww1.microchip.com/downloa...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

is it ok now

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

here  VCC is Either 5V or 3.3V

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The optos on the relays do nothing to improve your circuit, get rid of them. The optos on your inputs share 0V - that is also next to useless. If you want galvanic isolation, then have a separate isolated power supply for it. Something else you need to consider is stray currents across your power supply module - they're going to flow through your circuit and back out via routes you don't expect. You might want to add a capacitor to ensure these are shunted to earth.

 

Get your schematic correct first, then think about your pcb.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks kartman,

 

I will modify as your suggestions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some suggestions:

 

A good PCB requires a right schematic. Skipping the schematic and going straightforward to make your board is one of the biggest mistakes, this leads to inconsistency all over the project. You cannot just make global labels in the schematic and then ignore them totally in the board. also you can just jump around fixing the board without fixing FIRST the schematic. You have used ATmega328p, but it seems that you havent studied well the datasheet of this chip, you also skipped one of the most important datasheets which adresses the basics for hardware consideration when using this exact chip.

 

Making a PCB that works is something, Making a good PCB with good design is something else. In your case, its even more dangerous since you are dealing with high current, the outcome would be a disaster. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks  Moe123 ,

 

I will update My schematics at the Earlist

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi guys,

 

Schematics Modified as per your's valuable suggestions.

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PD2,3 look like they go out into the real world. How are you protecting them?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

to Interface Relay Module like PC0,PC1,PC2...only.

 

suggest me , if other circuit than Relay circuit, How to  protect.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

also,

 

ADC5 is used for Loop Detector  , which is from out side, if the loop detector, detect the vehicle(metal), it gives low input  otherwise high input, these inputs reads by ADC5.

 

if Atmega operated with 3.3V, Loop detector gives high(+5V), so that we used +3.3v to 5V voltage level converter.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Much better, however, never cross wires like this with a dot.  If the dot become hard to see (say a photocopy or pasted into a document) it will be easy to miss the connection.  Use 3-way connecting intersections & there will be no possible mistake.

Here, it might appear the 3.3V arrow only connects to the capacitor...when it's just signal lines, it would be even easier to miss.

 

 

Also, add a vcc arrow near C8, it becomes a lot easier to see where Vcc is coming from.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Thu. Aug 22, 2019 - 03:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks guys,

 

schematics attached, 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Move the Vcc slightly over, so not to cause possible confusion as to the connection. You might say these wires just happen to be crossing & Vcc only connects to the cap

 

 

This is how you want it---then there is NO possibility of any mix-up

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi 

 

schematics diagram attached, comments please , can I Start PCB.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

You might want to tell us about all the inputs and outputs - what you expect to connect to them, length of wiring, voltage etc. Then we might be able to help.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

You noticed this is the 5th or 6th time you are making the same mistake ?

 

 

Please dont mix:

- the 3V3 is the output of the LDO

- 5 V is the output of the step-down converter

 

Now you are feeding both the 5V and 3V3 to the connector, I suppose the CN9 is a switch or whatever to choose what you are feeding to MCU. why you are complicating it so much like this ? Vcc is a pin name in the MCU. 3V3 or 5V are two different voltage level. if you think its best then just choose another name for the Vcc(label NOT pin).

 

I also think the concept in general is bad. almost all the pins goes out to real world, nothing mounted on the PCB directly but through connectors....more wires, more EMI and noise, I doubt if this concept will ever pass the EMI test ?!.

 

Last Edited: Fri. Aug 23, 2019 - 07:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks  Moe123 ,

 

Label VCC changed as MCU_Power, 

 

Actually I want to Run MCU with 3.3V, In case it is not , then I can use 5V, 5V also using for Relay.In my Previous Design , used 3.3V as MCU power but did not Drive the Relay(same circuit), so that If 3.3V fails then , I can use 5V ,this is My concept. Please suggest on this.

 

Only ADC5(for loop detector), PD2, PD3 (only future use) are going out side, Please suggest , how to protect  from noise.

 

 

 

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Srinivas_nipun wrote:

thanks  Moe123 ,

 

Label VCC changed as MCU_Power, 

 

Actually I want to Run MCU with 3.3V, In case it is not , then I can use 5V, 5V also using for Relay.In my Previous Design , used 3.3V as MCU power but did not Drive the Relay(same circuit), so that If 3.3V fails then , I can use 5V ,this is My concept. Please suggest on this.

 

Only ADC5(for loop detector), PD2, PD3 (only future use) are going out side, Please suggest , how to protect  from noise.

 

 

 

 

 

 

But you are driving almost all the others with the same label "(Vcc)", so not just the MCU ?

  

 

So how does this work ?. Please open the schematic, take a calm look at it...trace the your 3V3...5V...etc and look if it make sense.

 

Srinivas_nipun wrote:

 

Actually I want to Run MCU with 3.3V, In case it is not , then I can use 5V, 5V also using for Relay.In my Previous Design , used 3.3V as MCU power but did not Drive the Relay(same circuit), so that If 3.3V fails then , I can use 5V ,this is My concept. Please suggest on this.

 

 

 

Then you SHOULD, before going further, to test it in real life.... see how can you drive this relay, if its 5V, then its 5V. Have you ever tried to test this concept in a small protytype ? just connect some wires and check!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I will change VCC as MCU power, It is okay , fine.

 

Board Already working fine in the Field, but some times  MCU hang, when the Power supply is off and On it is working , that is the issue, As per the Kartman, PCB Layout is Bad.

 

Also, found no galvanic isolation by opto couplers, then I Removed in my schematics. Now I am Expecting with Existing schematics, with 3.3V as MCU power , MCU input can drive the Relay or Not.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Srinivas_nipun wrote:
MCU input can drive the Relay or Not.

 

 

We cant tell you forsure! you have to check it by yourself. connect some wire, do some testing and read the datasheet of the relay. only then you can be sure.

 

Side note: What is your background in HW design/ Electronics ?? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Srinivas_nipun wrote:
but some times  MCU hang

 

But kartman also and the others traced a lot of problems and bad layout PCB. Kartman also gave you a lot of points why you should not only be bothered by the hang issue but also ommit and fix the obvious problem.

 

Are we going to have this same conversation all over again ?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Side note: What is your background in HW design/ Electronics ?? 

 

Both.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

then start do some HW testing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bad layout PCB

 

I used copper near by Relays, so that I removed My Lay out

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lets not just move away from the topic. It was not just the bad layout, check your 1st schematic and compare it with your 10th edition schematic. Now if you would like some help here then start to do some testing and re-evaluate your concept + taking the suggestions from the others, not just replying without doing some real work.

 

We dont get pay here, its a helping forum from volunteers!

Last Edited: Fri. Aug 23, 2019 - 09:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks Moe123

 

My first Schematics attached, Tested  Proto type Board. It did not drive the Relay with 3.3V as MCU power.

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

also Existing PCB Image Attached. hanging issue found in this PCB

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is Q1 doing? Q2,3 are reversed! I think there is too much guesswork happening here.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks kartman ,

 

if PC0 is high, then Q1 is on, then Relay coil(pin2) get the ground Path, then it is On.

if PC0 is at low, then Q1 is off , then relay is Off.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are you putting a "diode" at the HLK-5V?  Maybe to prevent  CN2 from forcing voltage into HLK5?  That's OK, then

 

Also, why do the wires reverse direction, that's ridiculous...get organized & draw from left to right.  Your power supply section is nicely drawn (other than Vcc & 3V3 improperly drawn as a 4-way dot).

 

470 ohms? for r1????...way too much

 

Q2 & Q3 are WRONG...redo them the emitters must go to gnd  R 3/4 should be maybe 500 to 5k ohms, not 10 ohms!

 

get a badge that says  Practice makes perfect.  it's worth the time to make it right.

 

You need a fuse at CN1...no AC line powered item should overlook that.   May be include a MOV (varistor) as well

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Kartman and  avrcandies , thanks.

 

that Schematics is Wrong,

 

updated Schematics is Attached,Expecting  yours suggestions on that.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you measure the leakage current between the 0V of the HLK-05m05 and mains earth using the AC current range of ,say 200mA using your multimeter? I'd be expecting 100 or so uA of leakage.

 

Tell us more about the inputs - there's a magnetic loop detector input which you say is relay contacts and is connected by 60m?? of cable. What relay is used here? The reason I ask is there is usually a minimum current required for switch/relay contacts in order for them to work properly. Once we know what relay, then we will have a minumum current required. This will guide further design decisions.

 

 

Last Edited: Sat. Aug 24, 2019 - 06:15 AM