# Tutorial 4 DIP Switch SPST and 7-segment LED Through Tinkercad

4,808

1

## Introduction: Tutorial 4 DIP Switch SPST and 7-segment LED Through Tinkercad

We are a group of UQD10801 (Robocon1) students from Universiti Tun Hussein Onn Malaysia (UTHM) that will show how to set up DIP switch SPST and 7-segment LED.

We had chose Tinkercad to design the circuit, Tinkercad provides pre-built circuits that can help users to not complicate their circuits by building from scratch.

DIP switch is a manual electric switch that is package with others in group in a standard dual in-line package (DIP). This type of switch is designed to be used on a printed circuit board along with other electronic components and is commonly used to customize the behavior of an electronic device for specific situations. In this circuit, 4 position DIP switches are used.

7-Segment LED Display consists of seven LED arranged in a rectangular fashion as shown. Each of the seven LED is called a segment because when illuminated the segment forms part of a numerical digit to be displayed. By forward biasing the appropriate pins of the LED segments in a particular order, some segments will be light and others will be dark allowing the desired character pattern of the number to be generated on the display. This allows us to display each of the ten decimal digit 0 through to 9 on the same 7-segment display.

## Step 1: Components Needed:

1. Arduino Uno R3

2. Resistor which is between the range of 160 to 533 ohm

3. DIP Switch SPST

4. 7-Segment LED Display

## Step 2: Connection of the Circuit :

1. Find all the components needed from the search box on right side in the tinkercad.

2. Connect the segment from a to g of segment LED Display to the pin of digital as output.

3. The pin of DIP Switch SPST also connected to the digital pin of Arduino as input.

4. Connect the circuit with the start of 5v at the power section, to the ground (GND) at the digital section.

5. Make sure the resistor is connected from the 5 volt straight forward instead of connected with GND.

6. Run the circuit.

## Step 3: Explanation of Coding

int a = 11; //For displaying

segment "a"

int b = 10; //For displaying segment "b"

int c = 7; //For displaying segment "c"

int d = 8; //For displaying segment "d"

int e = 9; //For displaying segment "e"

int f = 12; //For displaying segment "f"

int g = 13; //For displaying segment "g"

int s1 = 6;

int s2 = 5;

int s3 = 4;

int s4 = 3;

int s1state;

int s2state;

int s3state;

int s4state;

void setup() {

pinMode(a, OUTPUT); //A

pinMode(b, OUTPUT); //B

pinMode(c, OUTPUT); //C

pinMode(d, OUTPUT); //D

pinMode(e, OUTPUT); //E

pinMode(f, OUTPUT); //F

pinMode(g, OUTPUT); //G

pinMode(s1,INPUT_PULLUP);

pinMode(s2,INPUT_PULLUP);

pinMode(s3,INPUT_PULLUP);

pinMode(s4,INPUT_PULLUP);//switch

}

The segment of the coding above is used for declare each pin, input and input.

void displayDigit(int digit)

{

//Conditions for displaying segment a

if(digit!=1 && digit != 4)

digitalWrite(a,LOW);

This is the coding segment we give the instruction to the segment LED display for how is it display in different condition. As example form the coding above, we tell the system that if the digit not equal to 1 or 4, segment a of LED display should be LOW, mean turn off.

void turnOff()

{

digitalWrite(a,HIGH);

digitalWrite(b,HIGH);

digitalWrite(c,HIGH);

digitalWrite(d,HIGH);

digitalWrite(e,HIGH);

digitalWrite(f,HIGH);

digitalWrite(g,HIGH);

}

Segment above is used to reset the segment for every time the switch used.

void loop() {

The system will read it when the user presses the button of DIP Switch SPST. It sets in loop so that the process can be repeat many time when user keeps using this circuit.

if(s1state == HIGH && s2state == HIGH && s3state == HIGH && s4state == HIGH)

{ displayDigit(0);

delay(1000);

turnOff();

}

If…else function is used to set the number display when the different button is pressed by user. For this example, when the user press s1state, s2state s3state and s4state, the LED display will show number of 0. The coding will repeat for every condition from number ‘0’ to ‘9’.

## Step 4: Full Coding

int a = 11; //For displaying segment "a"

int b = 10; //For displaying segment "b"

int c = 7; //For displaying segment "c"

int d = 8; //For displaying segment "d"

int e = 9; //For displaying segment "e"

int f = 12; //For displaying segment "f"

int g = 13; //For displaying segment "g"

int s1 = 6;

int s2 = 5;

int s3 = 4;

int s4 = 3;

int s1state;

int s2state;

int s3state;

int s4state;

void setup() {

pinMode(a, OUTPUT); //A

pinMode(b, OUTPUT); //B

pinMode(c, OUTPUT); //C

pinMode(d, OUTPUT); //D

pinMode(e, OUTPUT); //E

pinMode(f, OUTPUT); //F

pinMode(g, OUTPUT); //G

pinMode(s1,INPUT_PULLUP);

pinMode(s2,INPUT_PULLUP);

pinMode(s3,INPUT_PULLUP);

pinMode(s4,INPUT_PULLUP);//switch

}

void displayDigit(int digit)

{

//Conditions for displaying segment a

if(digit!=1 && digit != 4)

digitalWrite(a,LOW);

//Conditions for displaying segment b

if(digit != 5 && digit != 6)

digitalWrite(b,LOW);

//Conditions for displaying segment c

if(digit !=2)

digitalWrite(c,LOW);

//Conditions for displaying segment d

if(digit != 1 && digit !=4 && digit !=7)

digitalWrite(d,LOW);

//Conditions for displaying segment e

if(digit == 2 || digit ==6 || digit == 8 || digit==0)

digitalWrite(e,LOW);

//Conditions for displaying segment f

if(digit != 1 && digit !=2 && digit!=3 && digit !=7)

digitalWrite(f,LOW);

if (digit!=0 && digit!=1 && digit !=7)

digitalWrite(g,LOW);

}

void turnOff()

{

digitalWrite(a,HIGH);

digitalWrite(b,HIGH);

digitalWrite(c,HIGH);

digitalWrite(d,HIGH);

digitalWrite(e,HIGH);

digitalWrite(f,HIGH);

digitalWrite(g,HIGH);

}

void loop() {

if(s1state == HIGH && s2state == HIGH && s3state == HIGH && s4state == HIGH)

{ displayDigit(0);

delay(1000);

turnOff();

}

else if(s1state == LOW && s2state == HIGH && s3state == HIGH && s4state == HIGH)

{ displayDigit(1);

delay(1000);

turnOff();

}

else if(s1state == LOW && s2state == LOW && s3state == HIGH && s4state == HIGH)

{ displayDigit(3);

delay(1000);

turnOff();

}

else if(s1state == LOW && s2state == LOW && s3state == LOW && s4state == HIGH)

{ displayDigit(6);

delay(1000);

turnOff();

}

else if(s1state == LOW && s2state == LOW && s3state == LOW && s4state == LOW)

{ displayDigit(0);

delay(1000);

turnOff();

}

else if(s1state == HIGH && s2state == LOW && s3state == HIGH && s4state == HIGH)

{ displayDigit(2);

delay(1000);

turnOff();

}

else if(s1state == HIGH && s2state == LOW && s3state == LOW && s4state == HIGH)

{ displayDigit(5);

delay(1000);

turnOff();

}

else if(s1state == HIGH && s2state == LOW && s3state == LOW && s4state == LOW)

{ displayDigit(9);

delay(1000);

turnOff();

}

else if(s1state == HIGH && s2state == HIGH && s3state == LOW && s4state == HIGH)

{ displayDigit(3);

delay(1000);

turnOff();

}

else if(s1state == HIGH && s2state == HIGH && s3state == LOW && s4state == LOW)

{ displayDigit(7);

delay(1000);

turnOff();

}

else if(s1state == HIGH && s2state == HIGH && s3state == HIGH && s4state == LOW)

{ displayDigit(4);

delay(1000);

turnOff();

}

else if(s1state == LOW && s2state == HIGH && s3state == LOW && s4state == HIGH)

{ displayDigit(4);

delay(1000);

turnOff();

}

else if(s1state == LOW && s2state == HIGH && s3state == LOW && s4state == LOW)

{ displayDigit(8);

delay(1000);

turnOff();

}

else if(s1state == LOW && s2state == HIGH && s3state == HIGH && s4state == LOW)

{ displayDigit(5);

delay(1000);

turnOff();

}

else if(s1state == HIGH && s2state == LOW && s3state == HIGH && s4state == LOW)

{ displayDigit(6);

delay(1000);

turnOff();

}

else if(s1state == LOW && s2state == LOW && s3state == HIGH && s4state == LOW)

{ displayDigit(7);

delay(1000);

turnOff();

}

}

## Step 5: Tutorial Video

Here is the video to to let have more understanding about the circuit.