Could digital pin 6 output interfere with analog pin 6 input?

I'm building a module based on someone else's circuit and code. The original design has a PWM output on digital pin 10, but to make the circuit easier to build I moved that function to digital pin 6. The module also has 100k potentiometers on analog inputs A0 to A7 and reads the voltages with analogRead.

For some reason the pot on A6 is being ignored, even though it seems to be wired correctly.

The original code has con2 = analogRead(6) / 64; to read the analog pin 6 and in another place it had `analogWrite(10, out1);' to write to digital pin 10, which I changed to analogWrite(6, out1); to account for my changes to the circuit. I wonder if this could be the cause of the problem.

I checked my wiring with a meter and it seems OK. I wondered if the two references to pin 6 were causing confusion, so I changed the analog read to con2 = analogRead(A6) / 64; to make it more explicit. However the problem remains.

Even stranger, the pot on A7 seems to be acting as if it was A6. Turning the pot on A7 has the same effect on the output result that the pot on A6 is supposed to have.

It should be possible to use analog pin 6 as input and digital pin 6 as PWM output at the same time, right? Do I need to make the digital output pin more explicit somehow?

Welcome to the forum

Please post your sketch, using code tags when you do

The project uses a nano - I assume you have the same.
The nano supports PWM out on D3, D5, D6, D9, D10, D11

From your description I'd be surprised if its not - partly at least - a hardware fault.
I'd suggest you make a simpler version of your sketch that JUST reads the analog inputs and prints the raw values to the serial monitor.

Meanwhile - you may WISH to post a schematic (always useful) - and your code in code tags.

The project's code uses timer registers for timer1. This timer works only with pins 9 and 10.

  TCCR1B &= B11111000;//fast pwm setting
  TCCR1B |= B00000001;//fast pwm setting
1 Like

Thanks for the info, Dlloyd. Much appreciated. Oh dear. I really feared that I might have to resolder the thing. Nothing else for it I guess, if it has to be 9 and 10.

Thanks for your help, John, I'll give that a try - make a simple sketch to confirm the inputs are reading correctly.
Here's the schematic:

The sketch is pretty long, but from what Dlloyd says it sounds like my switching pin D10 to pin D6 in the section below might be the problem:

void setup()
{
  pinMode(3, INPUT) ;//Z button
  pinMode(11, INPUT) ;//C button
  pinMode(10, OUTPUT) ;//accelx
  pinMode(9, OUTPUT) ;//accely
  timer1 = micros();
  timer2 = micros();
  TCCR1B &= B11111000;//fast pwm setting
  TCCR1B |= B00000001;//fast pwm setting
  //  Serial.begin(9600);

Hi John, as it turned out, a simple sketch confirmed the inputs were wired correctly. Weirdly, it seems it was trying to use D6 with the timer that was messing up A6/A7.
Update: And then again, maybe not. The problem has returned. Hardware issue is in play again.
Final update: Turned out to be a bug in the code from the project website! I'll be letting the original author know. Details in my follow up comment.

1 Like

Hi Dlloyd! With a bit of surgical snipping and jumpering I was able to rewire the output stage from D6 to D10 as per the original design, and the problem on A6/A7 went away. You were right on the money. Thank you so much!
Update: The problem has returned, even after rewiring to use pin 10. Something else is going on...will keep tweaking.
Final update: Turned out to be a bug in the code from the project website! I'll be letting the original author know. Details in my follow up comment.

1 Like

Turned out to be a bug in the code from the project website. The second CV channel was using con1 instead of con2, so both calculations were being affected bu the pot controlling con1:

//----------------------CV output buffer CH1-------------------------
  if (count1 == 1) {//attack
    if (atk1 <= 32) {//Minimize atk time
      i = 128;
      count1 = 2;
    }
    out1 = (pgm_read_byte(&(curve[con1][i])));//store output voltage value
    if (timer1 + atk1 <= micros()) {
      i++;
      timer1 = micros();
    }
  }

  else if (count1 == 2) {//release
    out1 = 255 - (pgm_read_byte(&(curve[con1][i - 128])));//store output voltage value
    if (timer1 + rel1 <= micros()) {
      i++;
      timer1 = micros();
    }
  }

  //----------------------CV output buffer CH2-------------------------
  if (count2 == 1) {//attack
    if (atk2 <= 32) {//Minimize atk time
      j = 128;
      count2 = 2;
    }
    out2 = (pgm_read_byte(&(curve[con2][j])));//store output voltage value
    if (timer2 + atk2 <= micros()) {
      j++;
      timer2 = micros();
    }
  }

  else if (count2 == 2) {//release
    out2 = 255 - (pgm_read_byte(&(curve[con1][j - 128])));//store output voltage value
    if (timer2 + rel2 <= micros()) {
      j++;
      timer2 = micros();
    }
  }

Funny side note:

If you use "pinMode(A7, INPUT);" on a Nano you will be changing the pinMode() of Pin 9.

Since A6 and A7 don't have associated digital I/O hardware the lookup tables for pinMode() don't go beyond A5 (Pin 19). By indexing off the end of the table it gets told that Pin 21 is on "PORTB". It then changes the Data Direction Register for PORTB bit 1 (Pin 9).

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.