How does this circuit work

I'm trying to teach myself some basic electronics and coding (eventually, hopefully) via a Mega 2560 Arduino kit. I've been working through some tutorials but i'm trying to understand the principles behind the circuits rather than just copying by rote. I discovered a tutorial for putting a tune through a passive speaker and noticed in the code that it also had active pins for LEDs (not mentioned in the accompanying description) and decided to attempt to wire them in.

It worked and they flashed with the music but then I noticed that I had forgotten to connect the wire i had in the negative rail to the ground on my board but the circuit was still complete and functioning. Is anyone able to give me any insight as to why this is please? I understood the ground to be like the negative pole of a battery so i'm unsure how the circuit is functioning without that connection. I've attached a circuit diagram I made on Fritzing and the code was as follows:

const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
 
const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;
 
int counter = 0;
 
void setup()
{
  //Setup pin modes
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}
 
void loop()
{
 
  //Play first section
  firstSection();
 
  //Play second section
  secondSection();
 
  //Variant 1
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 350);  
  beep(a, 125);
  beep(cH, 500);
  beep(a, 375);  
  beep(cH, 125);
  beep(eH, 650);
 
  delay(500);
 
  //Repeat second section
  secondSection();
 
  //Variant 2
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 650);  
 
  delay(650);
}
 
void beep(int note, int duration)
{
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);
 
  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  }else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }
 
  //Stop tone on buzzerPin
  noTone(buzzerPin);
 
  delay(50);
 
  //Increment counter
  counter++;
}
 
void firstSection()
{
  beep(a, 500);
  beep(a, 500);    
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);  
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
 
  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);  
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
}
 
void secondSection()
{
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);    
  beep(fSH, 250);
 
  delay(325);
 
  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);  
  beep(cSH, 175);  
  beep(cH, 125);  
  beep(b, 125);  
  beep(cH, 250);  
 
  delay(350);
}

I noticed that I had forgotten to connect the wire i had in the negative rail to the ground on my board but the circuit was still complete and functioning. Is anyone able to give me any insight as to why this is please?

Not without know which wire it was. Are you referring to missing GND wire in the Fritzing ?

It is possible that one of the pins connected to the LED's, could be a path for current to flow using the path from the pin, through the LED, through the resistor, through the buzzer to V+.

As far as I know, if a led or buzzer had no return path to ground it wouldn't work.

If you can reproduce this, show us a good image of the actual wiring.

If you can reproduce this, show us a good image of the actual wiring.

Larry,
Don't hold your breath.

:relaxed:

:grin:

Thanks very much for the replies guys! The Fritzing is one I made to show you the exact wiring I used rather than one I was following as the project page I was following only had a buzzer. The wiring was still in place as I did it just before bed.

I've attached a couple of photos from different angles that should be sufficiently clear to see. The white wire going nowhere is the one I realized I hadn't connected to ground, having already seen the circuit work. It plays Star Wars so it's hard to mistake.

My only explanation was similar to what Idahowalker said, that the current flows through the buzzer and back through the LEDs; as they flash alternatively, the code sets the output voltage of one to high when the other is low. Could the return current be switching back and forth to the low LED?

Please be patient if that sounds silly, I'm going off memory of 19 years old school Physics lessons and it wasnt my strongest subject, hence why I'm trying to strengthen this aspect of my skills/knowledge.

What is the resistance of your buzzer?

If it is low, (8-220 Ω) then there can be back-feed through it.

Ok Raschemmel, well I appreciate the help you've given me so far. I do understand how trying us newbies can be with our attempts to learn new skills.

Larry D, I'm afraid the documentation provided with the Elegoo Mega 2560 Starter kit is very sparse indeed and the resistance of the buzzer wasn't provided. I'm really enjoying learning what some of the possibilities are but a bit hamstrung by having only the most basic understanding of electronics/electrical theory.
Would I be right in saying that (although we may not know for sure) for the circuit to function it must be either that the LEDs (perhaps in turn as the voltage varies) or the buzzer represents the path of least resistance, creating a potential difference so that one or more outputs become the ground in function?

When the buzzer pin goes from HIGH to LOW (example: beep(f, 250); ) that potential would be passed through the buzzer to the GND wire of the buzzer which is connected to the GND rail of the prototyping board.

As a result the LED’s GND wires will see 5V and 0V at the frequency sent to the buzzer.

Connecting your GND to the ‘GND rail’ keeps a solid GND on your 3 circuit components and they work as expected.

Satisfaction brought the dead cat back to life. :wink:

raschemmel:
As far as I know, if a led or buzzer had no return path to ground it wouldn't work.

The other pin is the ground return! LED pin HIGH (= connected to Vcc), buzzer pin LOW (= connected to GND), and you have a circuit.