Attiny10 Digital Output Issue

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


Background: I am using the ATtiny10 PB0 to read (analog) the output of a MLX90242 Hall Effect sensor.  Based on that output if it is below 2V I set the digital output PB1 high and PB2 low, if the output is equal to or greater than 2V I set PB1 low and PB2 high.  A high value on PB1 turns on a green LED and PB2 turns on a red LED.  The issue is that the LED tied to PB1 never fully turns off, in the low state the pin shows 0.180V roughly instead of 0V.  However, when PB1 is high and PB2 is low PB1 shows 5V and PB2 shows 0V.  So why am I seeing 0.180V on PB1 in the low state and not 0V?  I believe the program is written correctly (done in AtmelStudio), but please review it below.  My circuit schematic is below as well.  I have tried adding a pull down resistor on PB1, but it did not pull PB1 to 0V, it stayed at 0.18V.  Additionally I increased R2 to 1k and still no change. 

 

#include <avr/io.h>

int main(void)

{

   

    // ADC channel 0

    ADMUX = (0 << MUX0);

    // Enable ADC, presc 1:8 for 125kHz ADC-clock

    ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);

    // PB1 & 2 output.

    DDRB |= (1<<1);

    DDRB |= (1<<2);

    while(1)

    {

        uint16_t analogData;

        uint16_t analogValue;

        // Start a conversion

        ADCSRA |= (1<<ADSC);

        while (ADCSRA & (1<<ADSC)); //wait until conversion is done

        analogData = ADCL; // copy ADC value to variable analogData

        analogValue = (analogData*5)/256; //convert back to a voltage 

        

        if (analogValue << 2)//Turn on Green LED if ADC is less than 2V

        {

            PORTB |= (1<<1);  //Set PB1 High

            PORTB &= ~(1<<2);  //Set PB2 Low

        }

        if (analogValue >= 2)//Turn on Red LED if ADC is equal to or greater than 2V

        {

            PORTB &= ~(1<<1);  //Set PB1 Low

            PORTB |= (1<<2);  //Set PB2 High    

        }

    }

}

 

Attachment(s): 

This topic has a solution.
Last Edited: Mon. Jun 29, 2020 - 02:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVRFreaks!

 

 

I don't see a 100nf bypass cap between vcc/gnd as required.

An LED needs about 1.8-2.0 volt or higher to turn on, so why would a low of 0.18v matter?

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

When initializing register use "=" rather then "|="

so instead of this:

DDRB |= (1<<1);

DDRB |= (1<<2);

Try this:

DDRB = (3<<1);

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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


I will add the 100nf bypass cap.  As for the 0.18V why does it matter, it matters because the LED is not turning off.  Some how that 0.18V is powering that LED I have no clue hence my post. See pictures below.  The left LED that is on is tied to PB2 which is logic level high, the right LED is tied to PB1 which is at logic level low shows 0.180V at the pin and the LED is on.

 

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

Jim I made that change see code below, now PB1 does not change states.  The LED stays off, and have a constant .4V on PB1.  PB2 still functions correctly when ADC reads >2 PB2 goes high to 5V red lead turns on when <2 it goes low to 0V and turns off.

 

#include <avr/io.h>

int main(void)
{
   
    // ADC channel 0
    ADMUX = (0 << MUX0);
    // Enable ADC, presc 1:8 for 125kHz ADC-clock
    ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);

    // PB1 & 2 output.
    DDRB = (1<<1);
    DDRB = (1<<2);

    while(1)
    {
        uint16_t analogData;
        uint16_t analogValue;
        // Start a conversion
        ADCSRA |= (1<<ADSC);
        while (ADCSRA & (1<<ADSC)); //wait until conversion is done
        analogData = ADCL; // copy ADC value to variable analogData
        analogValue = (analogData*5)/256; //convert back to a voltage 
        
        if (analogValue << 2)//Turn on Green LED if ADC is less than 2V
        {
            PORTB |= (1<<1);  //Set PB1 High
            PORTB &= ~(1<<2);  //Set PB2 Low
        }
        if (analogValue >= 2)//Turn on Red LED if ADC is equal to or greater than 2V
        {
            PORTB &= ~(1<<1);  //Set PB1 Low
            PORTB |= (1<<2);  //Set PB2 High    
        }
    }
}

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

I can not tell from that photo all the connections, but pull the yellow wire and measure the voltage to ground, my guess is it's higher then 0.18v.

An LED will not light up with that voltage.

By any chance is the LED in backwards and one end connected to the + rail by mistake?   As I said, I can not see the connections that well.

 

BTW,  I forgot to Welcome to AVRFreaks in the first post.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

stumpr84 wrote:
    DDRB = (1<<1);
    DDRB = (1<<2);

This will set DDRB so only PB2 is an output..

DO:

DDRB = (3 << 1);

as previously shown.

 

Edit:

Generally, first time you access a register, use "=", for subsequent changes use "=" or "|=" or "&=" as required for the specific circumstance.

David (aka frog_jr)

Last Edited: Fri. Jun 26, 2020 - 08:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

stumpr84 wrote:
 if (analogValue << 2)//Turn on Green LED if ADC is less than 2V

I missed this on the first read through:   the << in the above line should be <

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim and David,

 

First thank you for the help.  It looks like the main issue was in the "if (analogValue << 2)//Turn on Green LED if ADC is less than 2V" line of code.  I changed that to "if (analogValue < 2)" per Jim's suggestion and it now works correctly.  When I changed the initialization register DDRB to DDRB = (3<<1);  it had no effect on the issue. the working code is shown below  

 

Jim what is the difference between << and <?  I know in Arduino code << is a bit shift and < is a less than is it the same in AtmelStudio?

 

Again thank you both for the help.

 

*
 * PA0614 PCB Indicator Program.c
 *
 * Created: 6/24/2020 1:17:13 PM
 * Author : istumpe
 */ 

#include <avr/io.h>

int main(void)
{
   
    // ADC channel 0
    ADMUX = (0 << MUX0);
    // Enable ADC, presc 1:8 for 125kHz ADC-clock
    ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);

    // PB1 & 2 output.
    DDRB = (3<<1);

    while(1)
    {
        uint16_t analogData;
        uint16_t analogValue;
        // Start a conversion
        ADCSRA |= (1<<ADSC);
        while (ADCSRA & (1<<ADSC)); //wait until conversion is done
        analogData = ADCL; // copy ADC value to variable analogData
        analogValue = (analogData*5)/256; //convert back to a voltage 
        
        if (analogValue < 2)//Turn on Green LED if ADC is less than 2V
        {
            PORTB |= (1<<1);  //Set PB1 High
            PORTB &= ~(1<<2);  //Set PB2 Low
        }
        if (analogValue >= 2)//Turn on Red LED if ADC is equal to or greater than 2V
        {
            PORTB &= ~(1<<1);  //Set PB1 Low
            PORTB |= (1<<2);  //Set PB2 High    
        }
    }
}

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

 

When I took a quick look at your schematic I thought the 3 terminal chip was your voltage regulator (seeing 3 pins & Vout)

 

I was actually starting to type , why are you supplying your AVR power through a 10K resistor??!!? 

That made so little sense I stopped typing for another look...but I might not have if it were 20 ohms

 

Apparently there is a hidden 4-way connection & this is a perfect example of why you never draw a 4-way connection.

I knew a great example would show up soon.

 

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

Last Edited: Sat. Jun 27, 2020 - 01:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Another problem with 4-way ties:

Is there a junction here, or is it a cross over?

I "decided" it was a junction, but it "should" have been obvious without me having to guess.

 

Other junctions in the schematic are more distinct:

 

I am not quite as adamant as avrcandies about the 4-way ties, but this is a case where making the junctions VERY clear would eliminate a lot of possible confusion.

 

 

David (aka frog_jr)

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



I am not quite as adamant as avrcandies about the 4-way ties, but this is a case where making the junctions VERY clear would eliminate a lot of possible confusion.

In general, why not avoid it?  In fact, there is no reason TO draw it. There are too many ways to lose the dots.  You dig out a slightly faded print from 10 years ago... at a work site & take a picture of someone's schematic...at a meeting run some copies of a schematic someone brought along...maybe it's a copy of a copy..did the dots come through?  You just saw an example!

 

 

THIS BRINGS up another good point...even if THIS dot were horribly illegible, there is no problem since it is a 3-way connection!

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

Last Edited: Sat. Jun 27, 2020 - 03:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What I should have said is "... this is a case where making the junctions VERY clear (by using two 3-way ties) would eliminate..."

David (aka frog_jr)

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

avrcandies,

Sorry for the confusion on the schematic, but in my defense I do explain in my post what is going on and specifically say that the MLX90242 is a hall effect sensor not a regulator.

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

Hi frog_jr,

 

Thank you for your comments and I do understand the complaints about the schematic.  Unfortunately, some of the detailing was lost when I took a screen capture of it to show in the post.  The ExpressSCH software that I use is rather limited, but it gets the job done.  

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

Sorry for the confusion on the schematic, but in my defense

Don't take offense!

 

Besides helping you solve your immediate problem, the regular members on the Forum will generally try to help push you in the right direction to make you better at what you do.

 

As your projects, and code, become increasingly more complex having a solid foundation of good practices is important!

 

The cumulative number of years of experience on this forum that offer guidance is amazing.

 

 

 

Regarding drawing schematics,

 

I still draw wires that cross without connecting with little half circles "jumping" over the wire...

I think AVRCandies has a seizure every time I post one of those!

 

That's just the way I learned years ago, and sometimes old habits die hard.

He is trying to teach you best practices before you acquire to many bad habits!

 

As this schematic shows, Do as I say, not as I do...

Once they are etched into your brain, old habits die hard.

 

JC

 

 

 

 

Last Edited: Mon. Jun 29, 2020 - 03:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No offense was taken and I very much appreciate the comments and help.    

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

stumpr84 wrote:
Unfortunately, some of the detailing was lost when I took a screen capture of it to show in the post. 

This is exactly the reason for the recommendation to never have 4-way joins!

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Jun 29, 2020 - 04:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yea I got.