Interfacing 7-segment display and servo with Arduino not working

Hi,

My 12 year old son is trying to interface a 7-segment display (common anode) and a servo (sg90 micro) with Arduino Uno.

The objective is to increase the counter by 1, when the servo turns by 90 degrees. The servo is triggered by an IR sensor.

The issue is, counter fails to hold its value every time the servo is activated and resets to 0. He is using 1k resistor between the common anode of the 7-segment display and 5v of Arduino (current limiting resistor).

Request you to please assist. Thanks.

-Sanjay

If the display increments and then resets, I'd be inclined to check the code.

Maybe the power supply, drawing too much current and upsetting the regulator?

Post a schematic, the code, details of the power source, we can take a look.

Below is the sketch written by my son. Please note that he is still a novice to Arduino and programming.
Power source is Arduino’s 5 volts. I will post the schematic tomorrow. Thanks for looking into this.

#include <Servo.h>

int seg_a = 11;
int seg_b = 8;
int seg_c = 6;
int seg_d = 5;
int seg_e = 4;
int seg_f = 3;
int seg_g = 2;
int pot = A0;
int pot2 = A1;
int valuePot; //is set to analog value equivalent to 90 degrees
int valuePot2; //is set to analog value equivalent to 0 degrees
int valueServo = 0;
int ir_sensor = 7;
unsigned int i = 0; //global variable for the 7-seg display
Servo servo;

void setup() {

pinMode(pot, INPUT);
pinMode(pot2, INPUT);
servo.attach(9);
Serial.begin(9600);

//7-Seg LED pins
pinMode(seg_a,OUTPUT);
pinMode(seg_b,OUTPUT);
pinMode(seg_c,OUTPUT);
pinMode(seg_d,OUTPUT);
pinMode(seg_e,OUTPUT);
pinMode(seg_f,OUTPUT);
pinMode(seg_g,OUTPUT);

}

void loop() {
valuePot = analogRead(pot); //analog value equivalent to 90 degrees
valuePot2 = analogRead(pot2); //analog value equivalent to 0 degrees

valueServo = map(valuePot, 0, 1023, 2, 178); //maps 90 degrees to the variable
servo.write(valueServo);
delay(20);

if (digitalRead(ir_sensor) == 1){ //if an object is detected by the IR Sensor
i = i + 1; //value for 7-seg display
sevenSeg(); //calls the function to set the 7-seg display
valueServo = map(valuePot2, 0, 1023, 2, 178); //sets the servo back to 0 degrees
servo.write(valueServo);
delay(20);

}
}

//sets the 7-seg display based on the value of i

void sevenSeg(){

if(i == 0){
digitalWrite(seg_a,LOW);
digitalWrite(seg_b,LOW);
digitalWrite(seg_c,LOW);
digitalWrite(seg_d,LOW);
digitalWrite(seg_e,LOW);
digitalWrite(seg_f,LOW);
digitalWrite(seg_g,HIGH);
delay(2000);
}
else if(i == 1){
digitalWrite(seg_a,HIGH);
digitalWrite(seg_b,LOW);
digitalWrite(seg_c,LOW);
digitalWrite(seg_d,HIGH);
digitalWrite(seg_e,HIGH);
digitalWrite(seg_f,HIGH);
digitalWrite(seg_g,HIGH);
delay(2000);
}
else if(i == 2){
digitalWrite(seg_a,LOW);
digitalWrite(seg_b,LOW);
digitalWrite(seg_c,HIGH);
digitalWrite(seg_d,LOW);
digitalWrite(seg_e,LOW);
digitalWrite(seg_f,HIGH);
digitalWrite(seg_g,LOW);
delay(2000);
}
else if(i == 3){
digitalWrite(seg_a,LOW);
digitalWrite(seg_b,LOW);
digitalWrite(seg_c,LOW);
digitalWrite(seg_d,LOW);
digitalWrite(seg_e,HIGH);
digitalWrite(seg_f,HIGH);
digitalWrite(seg_g,LOW);
delay(2000);
}
else if(i == 4){
digitalWrite(seg_a,HIGH);
digitalWrite(seg_b,LOW);
digitalWrite(seg_c,LOW);
digitalWrite(seg_d,HIGH);
digitalWrite(seg_e,HIGH);
digitalWrite(seg_f,LOW);
digitalWrite(seg_g,LOW);
delay(2000);
}
else if(i == 5){
digitalWrite(seg_a,LOW);
digitalWrite(seg_b,HIGH);
digitalWrite(seg_c,LOW);
digitalWrite(seg_d,LOW);
digitalWrite(seg_e,HIGH);
digitalWrite(seg_f,LOW);
digitalWrite(seg_g,LOW);
delay(2000);
}
}

As he’s still a novice, get him to add some Serial.print lines in his code. This will tell him where values are changing, and what they are.

He has also tested with Serial.println(). The value doesn't increment when the servo is activated. Another observation, when the servo is delinked from the circuit, the 7-segment display works perfectly well.

My guess would be that the servo is taking too much power from the arduino's supply, perhaps causing a voltage drop. You could try putting a big capacitor (1000uf) across the supply.

Please find my son’s schematic attached. The circuit and the code work perfectly well in the Tinkercad Simulation Software.

(deleted)

Tried with a large capacitor in the simulation software. Capacitor was connected between Arduino Vcc and common anode of the 7-segment display. Seems to work well. Need to check in the real world. Hope this will not affect the Arduino or the 7-segment display. Please confirm.

Also, can the servo be powered by an external 9 volt battery? Is this too high for the servo? Thanks.

(deleted)

Power the servo with 4 x Rechargeable AA batteries (Eneloop or similar) and that will work a lot better. Connect the battery -ve to Arduino GND.

Steve

Thanks a lot for all your suggestions. Will surely try and let you know. Thanks again.

Dont forget to give karma for good suggestions shreyas, its the only payment we get!

Powering the servo with 4 x AA rechargeable batteries seems to work. Haven't tested it fully as Shreyas doesn't have the battery holder. Did some workaround to connect the batteries in series. This seemed to work. Difficult to source items due to present lockdown. Will definitely update after having tested it fully. Thank you all for the solutions.