I am using an Arduino Mega 2560 R3. I've written code for it that sets pin 20 as an external interrupt (attachInterrupt, 3, Change).
When I connect up a multimeter to the pin, it shows that there is a voltage of 5V on the pin at all times, when nothing else is connect to it. Why is this? I wouldn't expect an input pin to have a voltage on it.
I'm very much a new user and not an electrical engineer.
Let me add a few more details. We have a binary-to-decimal pushbutton wheel switch with its pins 1, 2, 4, 8 connected to arduino mega pins 8, 9, 10, and 11, respectively. We want our program to be immediately interrupted when a user changes the switch mode so we jumpered pin 8 on the arduino to pin 20 and made pin 20 an external interrupt. Since the arduino pin 8 is the 1s place on the binary switch, it changes any time someone hits the switch, so that change will send an interrupt into our void loop() and do its thing (check what the switch mode is now set to).
We have pull down resisters wired to pins 8, 9, 10, and 11. We found that even when arduino pin 8 should be at 0V (any even number on the switch), it was at 2.5 V. We disconnected the jumper to pin 20 and measured it and found it was at 5V, so that was why when they were connected, they read 2.5V (or 5 V, depending on the switch mode, but never 0V).
We can replace the pull down resistor to pin 8 with a smaller resistance, but I'd like to know why pin 20, which is not set to be an input pin, but should be by default, would read a voltage of 5V.
I suppose this makes sense now that you say so. We can't pull it down with software/arduino settings, right? We actually need to have a pull down resistor hardwired in?
I'm sure someone here knows, but I wonder if the internal pullup is enabled by default???? I was just adding a switch to my current project a few dys ago, and it seemed like it was working OK before I got-around to configuring the pullup. (I'm not sure... I didn't play around with it that much because I knew my code wasn't done yet, but it seemed odd.)
...but I'd like to know why pin 20, which is not set to be an input pin, but should be by default,
#include <avr/io.h>
#include <avr/interrupt.h>
/*
Arduino program with 3 different solenoid control profiles.
*/
int sol1 = 22;
int sol2 = 23;
int sol3 = 24;
int sol4 = 25;
int sol5 = 26;
int swt1 = 8;
int swt2 = 9;
int swt4 = 10;
int swt8 = 11;
int prog = 0;
void setup(){
pinMode(sol1,OUTPUT);
pinMode(sol2,OUTPUT);
pinMode(sol3,OUTPUT);
pinMode(sol4,OUTPUT);
pinMode(sol5,OUTPUT);
attachInterrupt(3,reset,CHANGE);// interrupt on pin 20
pinMode(swt1,INPUT);
pinMode(swt2,INPUT);
pinMode(swt4,INPUT);
pinMode(swt8,INPUT);
delay(2000);
readSwitch();
}
void readSwitch(){
prog = 0;
if (digitalRead(swt1) == HIGH){prog += 1;}
if (digitalRead(swt2) == HIGH){prog += 2;}
if (digitalRead(swt4) == HIGH){prog += 4;}
if (digitalRead(swt8) == HIGH){prog += 8;}
}
void reset(){
digitalWrite(sol1,LOW);
digitalWrite(sol2,LOW);
digitalWrite(sol3,LOW);
digitalWrite(sol4,LOW);
digitalWrite(sol5,LOW);
asm volatile("jmp 0");
}
void loop(){
switch (prog){
case 0:
break;
case 1:
delay(1000);
digitalWrite(sol1,HIGH);
digitalWrite(sol5,HIGH);
delay(1000);
digitalWrite(sol1,LOW);
digitalWrite(sol5,LOW);
break;
case 2:
delay(10000);
digitalWrite(sol1,HIGH);
delay(10000);
digitalWrite(sol2,HIGH);
delay(10000);
digitalWrite(sol1,LOW);
delay(10000);
digitalWrite(sol2,LOW);
delay(10000);
break;
case 3:
delay(10000);
digitalWrite(sol1,HIGH);
delay(10000);
digitalWrite(sol2,HIGH);
delay(10000);
digitalWrite(sol3,HIGH);
delay(10000);
digitalWrite(sol4,HIGH);
delay(10000);
digitalWrite(sol1,LOW);
digitalWrite(sol2,LOW);
digitalWrite(sol3,LOW);
digitalWrite(sol4,LOW);
delay(10000);
break;
}
}
We plan to swap out the 10Kohm pulldown resistor connected to arduino pin 8 (and 20) with a 1Kohm resistor. We've tested it on a breadboard and this works. I'm just still confused why pin 20 was outputting a voltage of 5V (or any voltage).
So the way it fails is that the interrupt is seldom triggered with a switch change, since the interrupt pin (arduino mega 20), sees oscillations between 2.5V and 5V instead of 0V and 5V like we thought it should.
Lok at the schematic of the mega I think the I2C line pin 20 has a pull up resistor on it.
Wire those switches correctly not with a pull down resistor. That's twice I have told you.
I'm completely new to pull up and pull down resistors. I read that tutorial and it still doesn't clear it up for me. I meant to ask in my last post: I'm not sure what you mean to wire the switch to ground with an internal pull up resistor. I enable the the pull ups on the arduino pins 8 through 11?
No no no.
Wire up the switch between the input and ground.
Then enable the internal pull up resistors.
Now that is your third strike.
If you still don't get it then you are not up to this task yet and you need to do some more tutorials and try and understand them.
Curtisrands: read up on pullup vs. pulldown resistors. Arduino has built-in pull-ups that you can engage in the pinMode command. As Grumpy Mike says, a pull-up based circuit would suit your purposes much better, especially given the built-in pullups on SDA and SCL.
Let me add a few more details. We have a binary-to-decimal pushbutton wheel switch with its pins 1, 2, 4, 8 connected to arduino mega pins 8, 9, 10, and 11, respectively. We want our program to be immediately interrupted when a user changes the switch mode so we jumpered pin 8 on the arduino to pin 20 and made pin 20 an external interrupt. Since the arduino pin 8 is the 1s place on the binary switch, it changes any time someone hits the switch, so that change will send an interrupt into our void loop() and do its thing (check what the switch mode is now set to).
I may be missing something here but if you are interested in knowing when the signal to a particular pin changes why don't you just use a 'pin change interrupt' (Arduino Playground - HomePage) ?
billroy:
Curtisrands: read up on pullup vs. pulldown resistors. Arduino has built-in pull-ups that you can engage in the pinMode command. As Grumpy Mike says, a pull-up based circuit would suit your purposes much better, especially given the built-in pullups on SDA and SCL.
Understood now. Thanks for the help. We removed our external pull-down resistors, enabled the pull-ups in the arduino (5 pins total) and rewired the switch "C" to ground. (Then adjusted our code slightly) Works great now and I understand pull-ups vs pull-downs on a pretty solid level now.
floresta:
I may be missing something here but if you are interested in knowing when the signal to a particular pin changes why don't you just use a 'pin change interrupt' (Arduino Playground - HomePage) ?[/color]
Don
I think we would have liked to know about the pin change interrupt a week ago when we first started on this. It most likely would have worked. For now though, we've got the code tested and running well with our new circuit so we are going to pass on it and use the external interrupts.