system
Given the following post, respond as if you're commenting on Reddit:
human
How do stoplights actually work?

I'm a delivery driver and just today while waiting a few miniutes at one did I actually question the science behind it. My question is what sensor is used to get a signal and what logic is behind the programming when it gets a signal?
gpt
To detect the presence of vehicles, the common solution is to use an inductive loop detector. An electrical wire is placed in the road underneath where the first vehicle would stop. A high-frequency alternating current is run through this wire. This alternating current produces a magnetic field that can affect ferrous objects near it through a mechanism called induction. If a large enough mass of ferrous material is present, this induction effect will alter the conductive properties of the wire loop. This is then detected and transmitted as a signal to the controller that a vehicle is present. For pedestrians, the sensor is typically just a push button. For bicycles, it could be either an induction loop or a push button.

As for the second part of the question, the logic behind the programming, there's no simple answer since there are many approaches. The most basic system will switch traffic lights on a fixed timer, completely independent of traffic. This requires no sensors. It's a very simple system, which works OK for intersections that have a low traffic load.

More advanced systems will use sensor data to influence traffic light switching, but there's no single way to do so. An obvious way to use sensor data is to keep the lights green for a specific flow of traffic as long as there are no vehicles detected in a conflicting flow. But ultimately, in situations with multiple conflicting flows, it depends on the design goals of the developer of the system as well as the goals of the government commissioning it. Such goals could include considerations like giving priority green lights to the high throughput main road rather than the quieter side roads. Or a "green wave" that links multiple traffic lights along a road together to allow a pack of vehicles to progress uninterrupted. Or encouraging/discouraging certain modes of transportation by prioritizing bicycle or pedestrian traffic over motorized vehicles. Or allowing public transport vehicles (bus/tram) to override the active signal planning (either by shortening the current phase to reduce the stopping time of the bus/tram or by immediately switching phase to give the bus/tram unobstructed passage through the intersection).