Not so simple LED switch

Hi projecteers, welcome to my first Phenix Project, this section is for reinventing old Electronic projects with more modern methods and components.

TE issue links

When I was younger I used to buy and endlessly pour over a series of electronic magazines put out by Talking Electrinics. These magazines were a fantastic resource back in the day for learning Electronics and had a lot of great projects and theory. If you get the opportunity go look at the old magazines here. Scroll down till you see the scanned links to the magazines and click on the links:

The first project I am going to reinvent was from Issue-1 and was originally called “LIGHT THE LED”, which apparently takes at least 30 min to solve. If you are into creating discrete electronic projects, or want to make your own circuit board they even include the pcb mask, to use for etching your own boards, the old fashion way 😉 .

This is a cute little brain teaser consisting of 2 switches and one red LED. The aim is to make the LED light up. There is no other indication that you are doing anything correctly till the LED lights up once entering the correct pattern. The circuit used a 4017 decade counter. The 4017 has 10 outputs one for each count, and sequentially change with each clock pulse.

from Talking Electronics Issue 1: Light The Led project
4017 Pinout

SW1 is de-bounced with 2 capacitors and feeds into pin 14 (Clock) on the 4017 chip. This will cause the output to go high in the pin order of: 3-2-4-7-10-1-5-6-9-11 (Q0 to Q9). Diodes D3 to D6 connect to the outputs of the 4017 chip, and will send a high back into pin 15 (reset) if SW2 is open circuit at that time, resetting the 4017 and causing Q0 to go high.

For our project we will use a Digispark ATtiny85 based breakout board. Mainly because it is small and as it only needs one digital output for the LED and 2 digital inputs for the switches we can easily achieve it with this board. The Digispark has 6 IO pins, however there is a caveat on the use of some of the pins. P1 is used for the onboard LED but can be used to drive an external LED, P5 is used as the Reset pin for the Digispark so it is best to forget about that pin as a digital IO, P4 and P5 are connected to the USB connector/traces and can be used with caution to not interfere with the programming of the Digospark.

Digispark Pinouts

For our project we will use P0 for SW1, P1 for LED, P2 for SW2. And design the software code to follow the same logic as the original TE project. As this is a proof of concept type project we will assemble the circuit on a bread board as shown below:

bread board circuit for our project

The logic of the code needs to follow the original idea of the TE project. Switch 1 (Right Switch above) will simple cause a counter to increment. To start with we will develop a program to simulate the counting action of the 4017 with a led connected to pin 11. Then we will add the reset logic later.

Open up the Arduino IDE and start a new project. Here i am assuming that you have already setup and tested it using the Digispark with the Arduino IDE, programming it with a simple blinking LED program is a good place to start to test the communications. If you have not then have a look at my tutorial here for setting up the Digispark with the Arduino IDE.

Copy the following code int the Arduino IDE editor and upload into the Digispark:

#define LEDpin 1
#define SW1pin 2
#define SW2pin 0

int counter = 0;
bool SW2state;


void setup() {
  // put your setup code here, to run once:

  //Set LED pin as digital output
  pinMode(LEDpin, OUTPUT);
  //Set Switch pins as digital inputs with pullup resistor
  pinMode(SW1pin, INPUT_PULLUP);
  pinMode(SW2pin, INPUT_PULLUP);

  //Make sure LED turns off
  digitalWrite(LEDpin, HIGH);

  //Wait 3 seconds and give a little hint
  delay(2000);
  //Flash the led 9 times to indicate nine SW1 pulses required to solve the puzzle
  for (int hint = 0; hint < 9; hint ++){
    digitalWrite(LEDpin, LOW);
    delay(100);
    digitalWrite(LEDpin, HIGH),
    delay(200);
  }

  // Turn off the LED to start the game
  delay(2000); 
  digitalWrite(LEDpin, LOW);

}

void loop() {
  // put your main code here, to run repeatedly:

//loop endlessly till button is pressed
while (digitalRead(SW1pin) == HIGH){}
    //A short delay to debounce the button
    delay(50);

    //Loop endlessly while the button is pressed
    while (digitalRead(SW1pin) == LOW){}
    //A short delay to debounce the button
    delay(50);
    //Read and store SW2 state
    SW2state=digitalRead(SW2pin);
       
  counter ++;


  //use case to enforce REset logic, to match the original circuit
  switch (counter) {
    case 1:      
      digitalWrite(LEDpin, LOW);
        if (SW2state ){ 
        counter=0; 
        }    
      break;

    case 3:      
      digitalWrite(LEDpin, LOW);
        if (!SW2state ){ 
        counter=0; 
        }    
      break;

    case 5:      
      digitalWrite(LEDpin, LOW);
        if (SW2state ){ 
        counter=0; 
        }    
      break;

    case 7:      
      digitalWrite(LEDpin, LOW);
        if (!SW2state ){ 
        counter=0; 
        }    
      break;
      
    case 9:
      digitalWrite(LEDpin, HIGH);
      break;
      
    case 10:
      counter=0;
      digitalWrite(LEDpin, LOW);
      break;
      
    default:

    break;
  }

} //End Loop

The solution for testing is D-U-U-U-D-U-U-U-U where D is SW2 closed circuit and U is open circuit. Remember to Close and Open SW1 to simulate the clock pin on the 4017.

Hope you enjoy this little project as much as I did, and thanks for reading this far.

Leave a Reply

Your email address will not be published. Required fields are marked *