When a current flows through the coil, the resulting magnetic field attracts an armature that is mechanically linked to a moving contact. The movement either makes or breaks a connection with a fixed contact. When the current to the coil is switched off, the armature is returned by a force approximately half as strong as the magnetic force to its relaxed position. Usually this is a spring, but the gravity is also used commonly in industrial motor starters.
Here is the relay circuit connection for microcontroller which is connected to TTL (I/O port):
In A, NPN transistor (say a BC337 or BC338) is being used to control a relay with a 5 V coil. Series base resistor R1 is used to set the base current for Q1, so that the transistor is driven into saturation (fully turned on) when the relay is to be energized. That way, the transistor will have minimal voltage drop, and hence dissipate very little power as well as delivering most of the 5V to the relay coil.
How do work out the value of R1?.
Let us say RLY1 needs 50mA of coil current to pull in and hold reliably, and has a resistance of 24 Ohms so it draws this current from 5V. Our BC337/338 transistor will need enough base current to make sure it remains saturated at this collector current level. To work this out, we simply make sure that the base current is greater than this collector current divided by the transistors minimum DC current gain hFE. So as the BC337/338 has a minimum hFE of 100 (at 100mA), we’ll need to provide it with at least 50mA/100 = 0.5mA of base current.
In practice, you give it roughly double this value, say 1mA of base current, just to make sure it does saturate. So if your resistance will be
TTL Logic High Voltage (Min) /1ma ( 1K approx)