I am working on a simple project using an Arduino Mega 2560 that involves reading a 12-bit binary encoder signal and which is used to cycle a digital pin high and low (depending on the angular location of the encoder).
My problem is that when the digital output pin is told to go low, it only goes down to about 4V. I also noticed that it would occasionally dip down to ground, instead of just 4V. Much to my surprise, when I unplug the Arduino, the digital output pin is still reading 4V. When I turned off power to my encoder which is connected up the the Arduino through 12 digital input pins, the output pin dropped down to 0V. When I turn the encoder back on it goes back up to about 4V.
I have tried connecting the output pin to ground and obviously the voltage goes to 0, but as soon as I remove the connection to ground the voltage springs back up to around 4V. It appears as though somehow the voltage being fed to the digital input pins (in the form of a ~5V digital input signal) is preventing the digital output pin from going to ground. I have no idea why this is the case, I have searched all over and could not find any similar complaints. My code is posted below, any help is greatly appreciated!
unsigned long CamAngle = 0; // Variable to store encoder value
unsigned long PrevCamAngle = 0; // Variable to store previous encoder value
int i = 0; // Cycle index
int BDC[] = {683,2048,3413}; // BDC angle (12-bit encoder value)
int TDC[] = {0,1365,2731}; // TDC angle (12-bit encoder value)
boolean TDCycle = false; // TDC Cycle
boolean BDCycle = false; // BDC Cycle
boolean Cycle = true; // Encoder rollover cycle (replaces TDC cycle 0)
void setup()
{
pinMode(37, INPUT); // Encoder bit 0 (PORTC-0)
pinMode(36, INPUT); // Encoder bit 1 (PORTC-1)
pinMode(35, INPUT); // Encoder bit 2 (PORTC-2)
pinMode(34, INPUT); // Encoder bit 3 (PORTC-3)
pinMode(22, INPUT); // Encoder bit 4 (PORTA-0)
pinMode(23, INPUT); // Encoder bit 5 (PORTA-1)
pinMode(24, INPUT); // Encoder bit 6 (PORTA-2)
pinMode(25, INPUT); // Encoder bit 7 (PORTA-3)
pinMode(26, INPUT); // Encoder bit 8 (PORTA-4)
pinMode(27, INPUT); // Encoder bit 9 (PORTA-5)
pinMode(28, INPUT); // Encoder bit 10 (PORTA-6)
pinMode(29, INPUT); // Encoder bit 11 (PORTA-7)
pinMode(30, OUTPUT); // Set PORTC pin 8 to output (stop pin float)
pinMode(31, OUTPUT); // Set PORTC pin 7 to output (stop pin float)
pinMode(32, OUTPUT); // Set PORTC pin 6 to output (stop pin float)
pinMode(33, OUTPUT); // Set PORTC pin 5 to output (stop pin float)
digitalWrite(30, LOW); // Set PORTC pin 8 low (stop pin float)
digitalWrite(31, LOW); // Set PORTC pin 7 low (stop pin float)
digitalWrite(32, LOW); // Set PORTC pin 6 low (stop pin float)
digitalWrite(33, LOW); // Set PORTC pin 5 low (stop pin float)
pinMode(53, OUTPUT); // Set pin 53 (PORTB-0) as digital output
digitalWrite(53, LOW); // Set pin 53 (PORTB-0) LOW
}
void loop()
{
// Cam Angle Update
PrevCamAngle = CamAngle; // Set variable to previous encoder value
CamAngle = (PINA << 4) + PINC; // Read encoder, set variable to value
if (TDCycle == true && CamAngle >= TDC[i]) // Wait for encoder to reach angle if cycle active
{
PORTB = 0; // Set digital pin 53 LOW
TDCycle = false;
BDCycle = true;
}
if (BDCycle == true && CamAngle >= BDC[i]) // Wait for encoder to reach angle if cycle active
{
PORTB = 1; // Set digital pin 53 HIGH
TDCycle = false;
BDCycle = true;
i++;
if (i > 2) // Reset every 3 cycles (3 cycles per revolution)
{
i = 0;
BDCycle = false;
Cycle = true;
}
}
if (Cycle == true && CamAngle < (PrevCamAngle + 1)) // Wait for encoder to cycle back to 0
{
PORTB = 0; // Set digital pin 53 LOW
BDCycle = true;
Cycle = false;
}
}
As you can see I am utilizing direct port manipulation, however I have the same issues when I use the Arduino library commands as well. I'm fairly certain this is some sort of hardware issue, however I could be wrong.