TinkerCad: Display Numbers on Seven Segment Display with Arduino

picture of title and arduino and seven segment display with TinkerCad logo

Using Arduino, Seven Segment Display and Embedded C, display numbers from 0 to 9 in a loop on TinkerCad.

Create an account on TinkerCad (if you don’t have one), then follow the following steps.

Creating the circuit on TinkerCad

  • Drag and drop a ardunio board, a 7 segment display and a resistors on your TinkerCad screen.
  • Connect the ‘Common’ of the 7 segment display to the terminal 2 of resistor and then connect the terminal 1 of the resistor to the ‘GND’ on arduino board.
  • Starting with point ‘A’ on 7 segment display, connect ‘A,B,C,D,E,F,G’ to points ‘2,3,4,5,6,7,8’ respectively (no hard and fast rule) on the arduino.

Now the circuit will look something similar to the following:

A picture of arduino connected with seven segment display in TinkerCad

Coding

Now go to the code section of the project. We need to update our code in order to display the numbers in loop.

Initialize variables as follows. Here, the variables a, b, c, d, e, f and g are the points on the 7 segment display and their respective values are the point numbers on the arduino with which they are connected.

int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;

Since, the points a, b, c, d, e, f and g are going to behave as an output i.e. display the numbers. We need to configure these pins to behave as an output using the following code.

void setup()
{
 pinMode(a,OUTPUT);
 pinMode(b,OUTPUT);
 pinMode(c,OUTPUT);
 pinMode(d,OUTPUT);
 pinMode(e,OUTPUT);
 pinMode(f,OUTPUT);
 pinMode(g,OUTPUT);
}

Now, inside void loop(), create a for loop that iterates from 0 to 10 and initialize the integer variables a1, a2, a3, a4, a5, a6 and a7.

void loop()
{
  int a1=0, a2=0, a3=0, a4=0, a5=0, a6=0, a7=0;
  for(int i=0; i<10; i++){
  ...
  }
}

The above for loop will be used to display numbers from 0 to 9. The variables a1 to a7 are used to set the value of pins to low or high (which will make the LED turn off and on).

Inside the for loop add the digitalWrite() function that carries two parameters -> pin and value.

void loop()
{
  int a1=0, a2=0, a3=0, a4=0, a5=0, a6=0, a7=0;
  for(int i=0; i<10; i++){
     ...
     digitalWrite(a,a1);
     digitalWrite(b,a2);
     digitalWrite(c,a3);
     digitalWrite(d,a4);
     digitalWrite(e,a5);
     digitalWrite(f,a6);
     digitalWrite(g,a7);
     delay(1000);
  }
}

The above code has 7 digitalWrite() functions that will make each individual LED turn on or off. But, to display numbers in the LED segment, we need to turn on only specific LEDs.

To display zero, we need to turn on all the LEDs except for the LED in the center middle. To display one, we need to turn on two LEDs on the right and rest should be turned off.

In order to achieve the above scenario, we will use switch case as follows:

switch(i){
     case 0:
      a1=1; a2=1; a3=1; a4=1; a5=1; a6=1; a7=0;
      break;
     case 1:
      a1=0; a2=1; a3=1; a4=0; a5=0; a6=0; a7=0;
      break;
     case 2:
      a1=1; a2=1; a3=0; a4=1; a5=1; a6=0; a7=1;
      break;
     case 3:
      a1=1; a2=1; a3=1; a4=1; a5=0; a6=0; a7=1;
      break;
     case 4:
      a1=0; a2=1; a3=1; a4=0; a5=0; a6=1; a7=1;
      break;
     case 5:
      a1=1; a2=0; a3=1; a4=1; a5=0; a6=1; a7=1;
      break;
     case 6:
      a1=1; a2=0; a3=1; a4=1; a5=1; a6=1; a7=1;
      break;
     case 7:
      a1=1; a2=1; a3=1; a4=0; a5=0; a6=0; a7=0;
      break;
     case 8:
      a1=1; a2=1; a3=1; a4=1; a5=1; a6=1; a7=1;
      break;
     case 9:
      a1=1; a2=1; a3=1; a4=1; a5=0; a6=1; a7=1;
      break;
    }

Full Code

int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
void setup()
{

pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
}
void loop()
{
  int a1=0, a2=0, a3=0, a4=0, a5=0, a6=0, a7=0;

  for(int i=0; i<10; i++){
    
    
    switch(i){
     case 0:
      a1=1; a2=1; a3=1; a4=1; a5=1; a6=1; a7=0;
      break;
     case 1:
      a1=0; a2=1; a3=1; a4=0; a5=0; a6=0; a7=0;
      break;
     case 2:
      a1=1; a2=1; a3=0; a4=1; a5=1; a6=0; a7=1;
      break;
     case 3:
      a1=1; a2=1; a3=1; a4=1; a5=0; a6=0; a7=1;
      break;
     case 4:
      a1=0; a2=1; a3=1; a4=0; a5=0; a6=1; a7=1;
      break;
     case 5:
      a1=1; a2=0; a3=1; a4=1; a5=0; a6=1; a7=1;
      break;
     case 6:
      a1=1; a2=0; a3=1; a4=1; a5=1; a6=1; a7=1;
      break;
     case 7:
      a1=1; a2=1; a3=1; a4=0; a5=0; a6=0; a7=0;
      break;
     case 8:
      a1=1; a2=1; a3=1; a4=1; a5=1; a6=1; a7=1;
      break;
     case 9:
      a1=1; a2=1; a3=1; a4=1; a5=0; a6=1; a7=1;
      break;
    }
    
        digitalWrite(a,a1);
	digitalWrite(b,a2);
	digitalWrite(c,a3);
	digitalWrite(d,a4);
	digitalWrite(e,a5);
	digitalWrite(f,a6);
	digitalWrite(g,a7);
	delay(1000);
    
  }
  
}

Output from TinkerCad

Github

Github link for the Embedded C program