ATtiny841 digital pin check for high with voltage applied, low if floating?

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

Hi there,

 

I'm a beginner with AVRs and electronics in general, and can't figure out how to do the following with the ATtiny841:

 

I can check if a pin is pulled to ground or floating (momentary switch between pin and ground). For this I use digital input with the internal pull up enabled. The pin check senses the difference between being pulled to ground or floating (pressing the momentary switch).

 

Now I have a situation where I want to check weather there is power on a pin or not. Like a momentary switch connected between the pin and battery + instead of between the pin and ground. How can I do this? With the internal pull up enabled, this pin is always being signaled as high, connecting to power makes no difference. If I disable the internal pull-up (tri state) the pin check constantly alternates between high and low when floating. If I connect to + I get a stable high readings, and if I connect to ground I get a stable low readings, but when floating the result is constantly alternating between high and low.

 

I don't want to use the ADC for this, I'd like to use the pin check for high on connection to +, low on floating. How can I solve this?

This topic has a solution.
Last Edited: Thu. Apr 13, 2017 - 09:23 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

10K external pull-down ... maybe

 

/Bingo

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

I guess my first answer is:  You cannot.

 

I've never run into this problem in practice.  I guess that might be because we design our AVR8 apps with the knowledge that they only have internal pullup facilities?

 

Akrylamid wrote:
I don't want to use the ADC for this, ...

 

Kind of ironic -- you start with the specification of an AVR model that has ADC on every port pin, along with differential capabilities across many pairs.  Yet you would dismiss that approach?!?  [that said, I can't think of how you might do it with ADC anyway]

 

If you choose an Xmega model, then you have more choices for pin driving:

 

12.3.1.1 Totem-pole with pull-down
In this mode, the configuration is the same as for totem-pole mode, expect the pin is configured with an internal pull-down
resistor when set as input.
 

The only thing I can think of is to use one of the pins e.g. that is part of SPI, such as SCK.  One can configure SPI for clock to idle high or low.  If the AVR driving of the idle-low SCK isn't as strong as your external +5 signal then it might well stay high.  Perhaps one could do similar with UARTs or I2C or analog comparator.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

theusch wrote:
Akrylamid wrote:
I don't want to use the ADC for this, ...

 

Kind of ironic -- you start with the specification of an AVR model that has ADC on every port pin, along with differential capabilities across many pairs.  Yet you would dismiss that approach?!?  [that said, I can't think of how you might do it with ADC anyway]

I was expecting a reply like this. I didn't bother to explain way as it's all experimental and for fun, if it can't be done I'll just use a voltage divider and read with ADC like I've previously done.

 

I was attempting the pin check to see if I could do the same without the ADC. When I am doing this check for power I'm running of a 22uF capacitor so every ounce of power is valuable. A pin change interrupt could detect battery power loss quicker than the voltage monitoring routine so I can go into power down and enable WDT interrupt with more cap capacity left. The WDT interupt checks for power restored, and I'm thinking that doing a pin check would consume less power than using the ADC. I'd like to test it anyhow...

 

It's just that I recall doing exactly this type of pin check with the 85V and it worked fine, and other people have done so too, so I thought it should be something rather simple... I guess it isn't.

Last Edited: Wed. Apr 12, 2017 - 08:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

10K external pull-down

+1

 

Agreed, sometimes one needs to know if an external voltage, (signal), is or is not present.

The signal is always a "High" if it is present.

e.g.: Is a back up battery, or a wall wart, plugged in?

e.g.: Is a backup generator running?

etc.

 

(Ignore low voltage conditions for now, for the sake of argument!

 

So yes, as mentioned, just tie the external input resistor to ground.

Now, if and only if the external signal, (voltage), is present, will you read a high.

 

JC

 

 

 

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

Akrylamid wrote:
It's just that I recall doing exactly this type of pin check with the 85V and it worked fine,

OK, I'll bite:  How was it done on the '85V?

 

Akrylamid wrote:
and I'm thinking that doing a pin check would consume less power than using the ADC.

Fair enough.  (but how will you do the check with the ADC without external components?  and won't a divider give you a constant load on your precious power?)

 

Didja consider the idea I gave, using a peripheral subsystem?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Akrylamid wrote:
Like a momentary switch connected between the pin and battery + instead of between the pin and ground.

Now you understand why people usually do connect the switch between the pin and ground ... ?

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

Not quite sure it this would apply; however, if you have a spare pin, put a resistor between that pin and the pin to test.

Leave this pin as input until you are ready to test, then drive it low to make a pull down on the pin to test.

 

Edit: Or place a cap on the pin to test with a resistor to the high/low source. Drive a low on the pin and then test to see if it is still low. The cap should hold the pin low if the high/low source is floating.

David (aka frog_jr)

Last Edited: Thu. Apr 13, 2017 - 02:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

take a NPN transistor and 2 resistors to "replace" your switch.

If there is a voltage on the pin the transistor is shorting the pin to ground if there is no voltage the transistor is not conducting and the pin is high.

Additional advantage is that your input is protected against voltages on the "line to measure" that are higher than the CPU voltage.

 

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

theusch wrote:
but how will you do the check with the ADC without external components?

I have external components. A voltage divider which I am monitoring battery voltage on one of the PORTA pins.

 

theusch wrote:
and won't a divider give you a constant load on your precious power?

No, because the divider comes directly from battery+. On battery power off the MCU is powered by the 22uF cap which is behind a diode.

 

awneil wrote:
Now you understand why people usually do connect the switch between the pin and ground ... ?

Yes, and I understood that before. But how does that help me check if battery power is back on? I used the switch example just to explain what I wanted to do. I guess that just complicated it even more...

 

It's comments like the above I don't get (but did expect). You don't need to know all of these details to answer the question. Either it can be done or it can't. And it can... Thanks Bingo and DocJC! I found two 100K resistors laying around, paralleled them together and used as a pull down... and it works perfectly.

 

So you see, there was no need to dig into ADC details and pick my project apart bit by bit, it was possible to answer the question very quickly and easily. Perhaps some of you could learn from that...

Last Edited: Thu. Apr 13, 2017 - 11:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Take the time to read some of the questions we get and maybe you'll understand where we're coming from. It's rare we get a well formed question we can give a well formed answer to.

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

Akrylamid wrote:
So you see, there was no need to dig into ADC details and pick my project apart bit by bit, it was possible to answer the question very quickly and easily. Perhaps some of you could learn from that...

How about this question?

 

theusch wrote:
It's just that I recall doing exactly this type of pin check with the 85V and it worked fine, OK, I'll bite: How was it done on the '85V?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

theusch wrote:

Akrylamid wrote:

So you see, there was no need to dig into ADC details and pick my project apart bit by bit, it was possible to answer the question very quickly and easily. Perhaps some of you could learn from that...

 

How about this question?

 

 

theusch wrote:

It's just that I recall doing exactly this type of pin check with the 85V and it worked fine, OK, I'll bite: How was it done on the '85V?

 


That was my mistake. I checked that driver and the pin I used had a resistor that was acting as a pull down. The pin input was from a voltage divider so it did not have a floating pin as I recalled.