I am trying to figure which one is the hot, neutral and ground wire. The voltage detector beeps when brought close to the wires with red caps(one red cap has three wires going in and another a single wire) and doesn't when brought close to the wires with yellow cap (two wires going into the cap). Though I see sparks when opening the yellow cap (and lights nearby blink). Is it possible to tell with this information? Any help is appreciated. Thanks!
The cap colors have nothing to do with it
The color codes say the **size and quantity* of wires they're designed for. For instance yellow can bind up to 3 14AWG wires, but red can bind up to 6. Their ranges overlap broadly.
It's all about the wire colors, except...
A bare, green, or green/yellow wire is always a ground. Not every installation has grounds. They are often pushed up into the back of the box.
A gray or white wire (light blue in Europe/rest of world) is a neutral, except it can be tagged with tape to use as a "hot". People often forget to tag the wires with tape, especially when the use is obvious.
Any other wire color can only be a hot. You should not mark a hot to be a neutral or ground. But you can't count on the last guy doing right.
So here's what we know
- The black-black bundle can only be hots.
- The white-white bundle is certainly the actual neutrals.
- The black-white bundle, the black must be a hot, so the white must be a hot that should have been marked with tape, and wasn't (typically because the use is "obvious"). Here, the obvious use would be for a switch loop.
In a switch loop, you have a black and (should be marked) white going down to the switch. The switch shorts them together to light the light. There's no standard which wire is always-hot and which is switched-hot. So realistically the switched-hot will be either wire.
Your lamp needs neutral and switched-hot. The neutral is easy; the switched-hot will be one of the others. You can guess, but if you guess wrong, the lamp will be on continuously and won't respond to the switch. That's exactly what you do to find out.
Ground wires are not readily visible in your photo. You'll have to look closer in the back of the box. Some light circuits are ungrounded; in which case there is nothing to attach to. Normally you have to replace a whole cable, not run a single wire; but as of 2014 it is legal to retrofit just a ground wire, if you really want to.
Common Switch leg configuration
The white/black wires twisted together is permanent hot -- turn off the breaker first if you wish and push it back out of the way. The two whites are neutral and the white on the fixture wires to them. The single black wire is a switched hot and is what the fixture black wires to. Way in the back are bare grounds twisted together. Those wire to the fixture bare copper ground.
The lights flicker because other lights share the neutral.
The yellow wire nut is your neutral. The grounding wire is always green or bare. The white that appears to be attached to a black is probably part of a switch leg. It should be marked with tape or another means of identification, but it would not be the first time it wasn't. The problem with a voltage detector is that it's a proximity detector. Meaning it detects voltage in the area of the box so unless you can separate the wires far enough apart, the voltage detector will signal on every wire in the box. You might want to check it out with a voltmeter which you can probably buy a cheap one for less than 20 dollars.
- The ground wire is the bare copper that is buried in the back.
- The neutral is the yellow capped bundle
- The lone red cap is likely your line from the power switch. You can test that by turning the switch on and off and seeing if there is power present (use a non-contact tester)
- The bundled red cap wires are pass through line power that is going to other lights, receptacles, etc.