# How to detect input voltage change on specific pin of atmega32? DC jack sensing

I am trying to make an LCD circuit that, eventually, will control an amplifier and display data to the user through the LCD. One of the features I want to include is the ability to show if the device is plugged via an AC wall adapter, or just a 9V battery. The circuit below is what I have so far. Everything is tested working on the bread-board except for the DC sensing jack.

Here is my circuit:

I am very new to the whole MCU programming thing, so I want to make sure I am on the right track. When I go to the program, will I be able to use pin 29, PC7, on port C to detect the change on my DC in port?

JP2 is the 9V while DC-IN is the wall adapter jack. Pins 2 and 3 are normally closed.

Basically, I want to detect when the user plugs in the DC adapter so I can change an icon on the LCD. When the user plugs in a wall adapter 2-3 on the DC-IN opens and PC7 will no longer have a connection to ground. (if I understand things correctly) That way I should be able to use PC7 to detect a change in R2, the pull-down resistor?

Basically, I still want to use PC0 through PC6 for other I/O stuff like led control and button press detection. I want to make sure that, programmatically, this is possible. That way I can continue my design knowing I can get things working.

• Also, it looks like I am missing the connection between VCC and r2 to make r2 a pull-down resistor. Sorry about that.. – John August Feb 23 '15 at 16:04
• Are you sure you've got the polarity of your DC jack the right way around? While there isn't a fixed standard, the way you have it seems wrong to me ... – brhans Feb 23 '15 at 17:01

What you are looking for is external interrupts. The chip that you are useing has hardware that can cause an interrupt if there is a logical change on some dedicated pins. Check out the documentation on page 66 about external interrupts. It looks like INT0 INT1 and INT2 can react to external interrupts if set (pins PD2, PD3 and PB2). You could be polling the PC7, but it's inefficient.

Basically, you need to utilize external interrupts futures of the chip, which are limited to certain pins. You write a interrupt subroutine and when interrupt happens, the subroutine is executed.

Also, check p.287 Voltage on any Pin except RESET with respect to Ground -0.5V to VCC+0.5V, thus you can not provide 9V to it.

• Okay cool, I will check out that portion of the documentation. That means I will have to move my LCD to another port, which is annoying, but should not be a big deal at all. I coded that so it would be easy to switch. – John August Feb 23 '15 at 16:30
• If your LCD is not using interrupts then it would be better to move it to regular pins, though you still have PB2 available. – Nazar Feb 23 '15 at 16:32

Your circuit as drawn should be able to do what you want.

In your code, I would enable the pull-up on PC7 like this...

PORTC |= _BV(7)


Here is how the pullup works...

Note that this assumes that DDRC7=0 (pin PC7 is input mode) and PUD=0 (pull-ups have not been disabled), which they should be as long as you don't change them in your code.

When there is no plug connected, then PC7 will be low because it is connected to ground though the jack. When a plug is inserted, PC7 is now no longer connected to ground though the jack and so the pull-up will pull the pin high.

You can detect this in software either by polling the PIN value of PC7 like this...

if (PINC & _BV(7)) {

// plug is connected

}


If you already have a polling loop in your program, then this should work fine. If, however, you want to be able to detect a change in the jack state asynchonously (that is, you don't want to have to keep checking it, you just want to know when it changes), then you will not be able to use pin PC7. Instead, you will need to connect the jack sense line to one of the external interrupt pin which are INT0, INT1, and INT2. You can then set up an interrupt to fire anytime the pin changes- and you can even have the interrupt wake you from sleep. Add a comment if you needto switch to interrupts and I'll edit with some example code.

On a side note, keep in mind that using the jack's switch pin in this way will only detect if a plug is physically inserted into the jack. It will not detect if that plug is actually supplying power. Happy to add some ideas on how to actually detect the currently active power source if you are interested.