Input Pin not working with ATTiny85 design

Hi Guys

I wasn't sure where to post this question, as it could be a programming or electronics query. Apologies if its in the wrong forum and if a Mod could move it to the correct place - thanks!

Here's my problem:

I designed and made a pcb on which an ATTiny85 with 5v voltage regulation is installed. Connections are provided to enable connections to and from the data pins of the ATTiny (plus a 10k resistor holding the reset pin high). I programmed the ATTiny before soldering it to the board and all worked well in its role as a general purpose led controller.

Next I redesigned the board layout by adding 6 pins, to allow onboard programming, and an ULN2803 to enable the ATTiny to drive more leds. Again, the new design all worked well.

However, now I need to program the ATTiny with a sketch that fades from one set of leds to another, requiring an input (from a switch). I assumed I could use one of programming pins as an input, because the programming pins are connected to the ATTiny before the transistor array, but here's the problem - the switch has no effect. The switch was wired between a programming pin (MOSI, pin 0) and ground.

The sketch used is one that worked perfectly on a UNO, but when used with the ATTiny (with relevant pin number changes), it doesn't work at all. It seems that the switch input is not being seen. I know that the switch pin is being configured as an input ok, as I've invoked the internal pull-up resistor on that pin and the pin is definately being held high (there's sufficient voltage at the pin to drive an led) and definitely goes low when the switch is operated, so what I'm asking is, can anyone see if have I missed a blatantly obvious mistake in the (albeit rather naive) programming, or is it a wiring misdesign?

Here's the code:

/* 
 **************************************************************
 * LED fade sequence using ATTiny85 *
 **************************************************************
 
 Physical Connections:
 
 Red leds : + to B2+, - to B2- (OP pins)
 Blue leds    : + to St+, - to St- (OP pins)
 Switch       : across MOSI and 0V (Prog pins)
 
 */

// Declarations

int redPin = 1;                                 // Red led o/p is connected to pin 1 (chip pin 6, pad B2)
int bluePin = 4;                                // Blue led o/p is connected to pin 4 (chip pin 3, pad St)
int WswitchPin = 0;                             // Switch is connected across pin 0 (chip pin 5, pin MOSI) and 0V
int Wval;                                       // Variable for reading the switch pin status
int Wval2;                                      // Variable for reading the switch delayed/debounced status
int WbuttonState;                               // Variable to hold the button state
int WMode = 0;                                  // Variable to hold switch status
int SeqNo = 0;                                  // Variable to hold next function selection


//SetUp
void setup() {

  //Serial.begin(9600);

  pinMode(WswitchPin, INPUT_PULLUP);            // Set the switch pin as input and enable internal pull-up resistor
  pinMode(redPin, OUTPUT);                      // Set the red pin for output
  pinMode(bluePin, OUTPUT);                     // Set bluie Pin for output
  digitalWrite(redPin, LOW);                    // Set red off
  digitalWrite(bluePin, LOW);                   // Set blue off 

}

void loop()

{
  // TASKS  	        	 
  WMode = 0;                                    // Set switch status to OFF
  Wval = digitalRead(WswitchPin);               // Read switch input value and store it in Wval
  delay(10);                                    // 10 milliseconds is a good amount of time to wait
  Wval2 = digitalRead(WswitchPin);              // Read the input again to check for bounces
  if (Wval == Wval2) {                          // 2 consistant readings?
    if (Wval != WbuttonState) {                 // The button state has changed
      if (Wval == LOW) {                        // Is the button pressed?
        if (WMode == 0) {                       // Is the switch status OFF?
          WMode = 1;                            // Then turn status ON
          ++SeqNo;                              // Increment the task counter by 1
        }
      }
      WbuttonState = Wval;                      // Save the new state in status variable
    }
    //Fade up to red
    if (WMode == 1) {                           // Check to see if button has been pressed and debounced

      if (SeqNo == 1) {                         // Is Task number 1 wanted?

        for (int i = 0; i <= 255; i++){         // Fade red led from off to fully on
          analogWrite(redPin, i);
          delay (10);
        }

      }
      //Fade to blue
      else if (SeqNo == 2){                      // Is Task number 2 wanted?

        for (int i = 0; i <= 255; i++){          // Fade from red to blue
          analogWrite(bluePin, i);
          analogWrite(redPin, 255 - i);
          delay (6);
        }    
      }

      // Fade to Impulse
      else if (SeqNo == 3){                        // Is Task number 3 wanted?

        for (int i = 0; i <= 255; i++){            // Fade blue to red
          analogWrite(bluePin, 255 - i);
          analogWrite(redPin, i);
          delay (6);
        }
        SeqNo = 1;
      }    
    }
  }  
}

the circuit diagram is here:

Thanks
Bernie

I can't read the diagram - and photobucket takes ages. Perhaps you can post a link that just shows your jpg?

Can you write a short sketch that just does something when the pin is high or low - like light an LED (on another pin) when it is high?

...R

Not sure if this is any better Robin2:

Its a bit of a fuzzy .jpg I'm afraid.

I did try a simple led on/off sketch, but that didn't work either. It was late last night though, so I will repeat it tonight before I get tired!

The code was working perfectly when used with a UNO, and the pcb works perfectly as an output only board, its only introducing an input thats giving me problems. I'll also try breadboarding an ATTiny with a simple switch on the same pin as well and see what happens. I just can't see any reason why that configuration wouldn't work :~

Thanks though :slight_smile:

Bernie

Well, I tried re-breadboarding the circuit - and it works perfectly every time, but only on the breadboard. :~.

I think this thread had better be moved to the electronics area, as the only difference I have been able to detect between the breadboarded circuit and the actual pcb (as per the wiring diagram previously referenced) Photobucket | Make your memories fun! or
Dropbox - File Deleted - Simplify your life , is that when the switchpin is held high by the internal pullup resistor, on the breadboard I can measure 5v on the pin, but on the actual pcb it only reads 1.6v. I can't see any reason at all why that should be, but it explains why the switch operation has no effect - there's not enough voltage drop when the switch is closed. This happens no matter what sketch I run and no matter whether I supply 12v through the voltage regulator or 5v onto the 5v line.

Can anyone see why this could be happening please??

Thanks
Bernie

BTW, how can I get this thread moved to the electronics area? Mods?

The ULN2803A has a 2.7-k ? series base resistor for each Darlington pair for operation directly with TTL or 5-V CMOS devices.

Your pushbutton pin is also connected to IN1 / 1B on the ULN2803. My guess is that the internal pull-up and the series base resistor + transistor act as a voltage divider.

Thanks CB :slight_smile:

Yes, I sort of suspected the connection to the array could be causing an effect as well, so I made sure to include the ULN2803 on the breadboarded circuit, with the connection made between the ATTiny pin and the array, and a connection between ground and the array ground pin but the breadboard circuit still works ok.

I 've even assembled a new pcb, just in case the first was faulty, but the second shows the same low voltage at the switchpin (its 1.16v not 1.6v as I wrote earlier). The pcb itself has been checked for shorts etc, but its all good and matches the circuit diagram exactly.

I think I may have to admit defeat here and design a new pcb with isolated inputs!

Thanks
Bernie

STDummy:
Yes, I sort of suspected the connection to the array could be causing an effect as well, so I made sure to include the ULN2803 on the breadboarded circuit, with the connection made between the ATTiny pin and the array, and a connection between ground and the array ground pin but the breadboard circuit still works ok.

Then you got the breadboard circuit wrong. The ULN2803 input and the attiny internal pullp resistor will definitely act as a voltage divider, as CodingBadly says.

OK, I'll try it again tonight, although the breadboard circuit was just to reproduce the problem to find a solution. Trouble is, if I can't reproduce the problem..... :wink:

The voltage divider answer is undoubtedly correct, so I'll try cutting the pcb track leading from the ATTiny to the array, as that pin is only going to be used for input anyway.

Thanks for helping me on this one guys :slight_smile:

Bernie

The two resistances and the HI-LO threshold for the input will vary from chip to chip. I suppose it's possible you found a pair of chips that works together and another pair that don't.

Well, I cut the track between the switch input pin and the ULN2803 and guess what? It now works ok XD
Suspicions confirmed! As you say CB, it could well be that the breadboarded circuit just happened to have tolerances that allowed it to work with the array connected (it still works!), but I now know what to look out for next time.

Thanks again guys :slight_smile: