The Sun bombards the Earth with fast moving charged particles. We call the emissions of these various high energy (fast) particles solar wind. These charged particle don’t tend to reach the atmosphere because the Earth has a magnetic field.
A particle with charge q and velocity v in a magnetic field B experiences a Lorenz force F=qv×B where x is a vector product of the velocity and the local filed. (Of course the sign of charge can flip the direction of the force.) The vector multiplication will provide a force thats perpendicular to both the local filed and the velocity (even when those last two aren’t perpendicular in that case the vector product will give us a smaller force). So what tends to happen to these charged particle is that their direction of motion is changed by the magnetic filed.
The magnetosphere of the Earth is a really complicated thing which is also shaped by the charges interacting with it, moving (also accelerating) creating their own magnetic fileds which makes the field really hard to model as the field changes the motion of charges in it and moving charges change the field. So what is actually going on is poorly understood but the wiki does go into this in more detail: https://en.m.wikipedia.org/wiki/Aurora
So without too much of the hows in some circumstances these fast charged particles can reach the higher atmosphere in large numbers. These particles are mostly protons and electrons. When electrons collide with an electron of an atom usually 3 things can happen based on how much kinetic energy the electron had relative to the other electron in the atom. The electron can knock the other electron out of the atom, this is ionisation. It can undergo elastic collision, this is more or less a nothing-burger, and it can also undergo inelastic collision which is a really interesting process.
In the inelastic collision case the energy difference goes into making the electron in the atom jump to a different orbital with a higher energy state. In a given atom a jump from one to an other specific state requires a precise anount of energy. But now there is a lower energy state where that electron used to be so from this excited state the electron will jump back to the lower energy state relativity quickly and crucially emits 1 photon with a specific frequency (colour). The light emitted doesn’t happen continuously so for a given transition between two states we get 1 emitted photon with some frequency. (The spectral lines do have some width but thats beyond our scope here.)
If the emissions were to be continuous we wouldn’t see specific colours but it isn’t so we do.
Latest Answers