ULN2003 as Relay Driver
Individual darlington pair configuration in ULN2003 ic is given on the right side. 3.3 volts from the output pins of stm32 microcontroller goes to the base of first transistor. First transistor emitter is input to second transistor base. So when first transistor is switched on instantly the second will also becomes switched on. Current will start flowing from ‘Out’ to ground. Diode at the ‘COM’ pin is working as a fly back diode. It prevents the circuit from any back emf generated by the relay coils. ULN2003 can handle loads requiring 50 volts and 500 mA of current.
I am going to switch on and off four relays with stm32 microcontroller. Four buttons are used as input to stm32 microcontroller. These four buttons corresponds to four output relays. Pressing each button will change the state of its corresponding relay. For example if the relay is on pressing its corresponding button will change its state to off. We can say that buttons are toggling the state of relays on each press.
Microcontroller used in the project is stm32f103c8t6. Its build on cortex m3 core. Port-A pins 0,1,2,3 of stm32 microcontroller are used as output pins. Pin#0 of port-a is connected to input 1 of ULN2003 relay driver. Pin#1 of port-a is connected to input 3, pin#2 is connected to input 5 and pin#3 is connected to pin#7 of ULN2003 relay driver.
Input buttons are connected to port-b pins 6, 7, 8, 9. Stm32 microcontroller has build in pull up and pull down resistors on each individual gpio pin. We can enable and disable them in code. For our button inputs i enabled the pull up resistors associated with each gpio(Port-B Pin#6,7,8,9). One side of input buttons is connected to microcontroller pins and the other side is commonly grounded.
Relays which i am using in the project activates on +9 volts. I connected the relay coil one end with ULN2003 output and the other +12 volts. Com pin of relay is grounded with the load power. Nc pin of relay is connected with load and other end of load is connected with positive lead of load power. No pin of relay is left untouched. Ground pin of ULN2003 is grounded with relay 12 volt power supply and with stm32 ground. Note ULN2003 GND must be commonly grounded with stm32 power supply and ULN2003 relay power supply.
if(HAL_GPIO_ReadPin(GPIOB,B6_Pin)==GPIO_PIN_RESET) //Check for input button press
The code above can be used with the relay board. Connections are same. Only you have to make relay power and stm32 power grounds common.
Filed Under: Electronic Projects, STM32