⚙
MPMC Lab · Experiment 8 · 8051 · Embedded C

Relay, Buzzer, Switch and LED Interfacing Using Embedded C

Drive a relay and a buzzer through transistors, and select actions with two switches.

☰ All experiments

Aim

To write Embedded C programs to switch a relay and a buzzer ON and OFF with a time delay, and to interface two switches (P3.0, P3.1), a buzzer (P2.0), a relay (P2.1) and LEDs (Port 1) so that the switch inputs select: flash LEDs, buzzer ON/OFF, relay ON/OFF, or a 3-bit counter on the LEDs.

Learning objectives

1
Driver circuits
Explain why relays and buzzers need a transistor/ULN2003 driver and a flyback diode.
2
sbit
Control single pins with sbit declarations.
3
Switch decoding
Combine two input bits into a mode number and dispatch with switch-case.
4
Timing
Choose delays suited to mechanical relays (≥ 10 ms) and audible buzzers.
5
Safety
Recognise the hazard of mains-voltage loads on the relay contacts.
6
Testing
Verify every input combination and record the observed behaviour.

What this experiment demonstrates

  • Interfacing loads that need more current than a port pin can give.
  • Polling inputs and using a state (mode) to select behaviour.
  • The audible and mechanical feedback of real actuators.

Sub-tasks

  • 8.1 Relay ON/OFF with a time delay
  • 8.2 Buzzer ON/OFF with a time delay
  • 8.3 Switch-selected actions (8.4–8.7)

Driving loads

8051 P2.1 1 kΩ BC547 / ULN2003 GND coil +5 V flyback diode 1N4007 Why a transistor? A port pin gives < 1 mA high / ~10 mA low.A relay coil needs 70–90 mA, a buzzer 20–30 mA. The diode absorbs the inductive kick (L·di/dt)when the coil switches off; without it thetransistor and the MCU can be destroyed.
Fig 3. Transistor driver for a relay or buzzer, with a flyback diode.

Software delay in C

Machine cycle T = 12 / f_osc = 12 / 11.0592 MHz = 1.085 µs (classic 12T core) Inner loop for(j=0; j<115; j++); ≈ 8 machine cycles per pass 115 × 8 × 1.085 µs ≈ 1.0 ms delay_ms(500) ≈ 0.5 s

The exact count depends on the compiler's optimisation level, so calibrate it with Keil's stopwatch (t1 in the status bar) or this simulator's clock. The often-quoted value 1275 was calibrated for a 1-clock DS89C4x0 chip. On a classic 12-clock 8051 it gives about 11 ms per count.

Mode selection

mode = (SW1 << 1) | SW0; /* 2 input bits → 0..3 */ switch (mode) { case 0: flash LEDs (SW1=0, SW0=0) case 1: buzzer on/off (SW1=0, SW0=1) case 2: relay on/off (SW1=1, SW0=0) case 3: 3-bit counter (SW1=1, SW0=1) }
Note for verification: the original list gives SW1 = 0, SW0 = 0 for every operation (8.4–8.7), which must be a typo. This lab assumes the four combinations above. Confirm the intended mapping with your instructor and edit the case labels if it differs.

A switch that is ON connects its pin to ground, so it reads 0. With both switches OFF the program reads mode 3.

Board wiring (this lab)

DevicePinLogic
SW0, SW1P3.0, P3.1ON = 0
BuzzerP2.01 = ON (via transistor)
RelayP2.11 = energised (via ULN2003)
LEDsP1.0–P1.71 = ON

Apparatus & environment

ItemSpecificationQty
PC with Keil µVisionC51/A51 tool chain (evaluation version is fine)1
8051 trainer boardNuvoton W78E052D, 11.0592 MHz crystal1
Nuvoton ISP-ICP utilityAPROM programming over USB-UART (CH340)—
USB cableUSB-A to the board's serial/ISP port1
Power supply+5 V regulated (board adapter)1
Relay module5 V SPDT relay with ULN2003/BC547 driver1
Buzzer module5 V active buzzer with transistor1
Switch & LED modules2 switches, 8 LEDs1

Safety rules & good practice

  • ⚡ Switch the board off before connecting or removing modules, ribbon cables or jumpers.
  • 🔌 Check the supply: +5 V only. Reverse polarity or more than 6 V destroys the controller.
  • 🧲 Handle ICs by the edges and touch a grounded surface first (ESD).
  • 🔁 Do not drive a port pin connected to a switch with 0 while the switch is pressed to VCC. Read inputs only after writing 1s.
  • 🔥 If a component gets hot or smells, switch off immediately and inform the instructor.
  • ⚠ Never connect mains (230 V AC) loads to the relay in the student lab. Use a low-voltage lamp or the relay LED only.
  • 🔊 Keep the buzzer volume low and do not leave it beeping continuously.

Procedure (hardware / real tools)

  1. Connect the relay to P2.1, the buzzer to P2.0, the LEDs to Port 1 and the switches to P3.0/P3.1 with the power off.
  2. Build and program 8.1 (relay). Listen for the click and watch the relay LED.
  3. Build and program 8.2 (buzzer).
  4. Build and program 8.3. Set each of the four switch combinations and record the behaviour in the table.
  5. Measure the relay ON time and the counter step time.
Click a step to tick it off. Your progress is saved in this browser.

Doing it in this virtual lab

  1. Choose a sub-task chip. The program opens in the µVision window.
  2. Press 🔨 Build (F7). Errors are listed in Build Output: click one to jump to the line.
  3. Press ▶ Run (F5) and watch the board. Use ¼× to 1/1000× slow motion to see fast events.
  4. Use Step (F11) and Step Over (F10), and click line numbers to set breakpoints.
  5. Edit the code (delay, pattern, pins), rebuild, and observe the change. Press ● Record to log results.
  6. Enable 🔊 Sound to hear the buzzer and the relay click.

Pre-Lab Test 6 questions · diagnostic

Check your prerequisite knowledge. Each answer gets instant feedback. Score at least 4/6 before using the simulator.

Observation table

Rows are added by ● Record in the simulator: inputs, the expected (theoretical) value and the simulator result. Type your own hand calculation in "Your value" to check it. Add rows by hand for hardware-bench readings. The table is saved in this browser.

#Sub-taskInputsExpectedSimulatorYour valueCheckNote

Edge cases to test

  1. 8.1: delay_ms(2): watch the relay chatter warning.
  2. 8.3: remove one break; and see the next case also run.
  3. 8.3: delete SW0 = 1; SW1 = 1; after writing 0 to them: the switches can no longer be read.
  4. 8.3: change a case to case 4:: can it ever be reached?
  5. 8.2: drive the buzzer at 1/100× speed: notice the timing of each beep.

Analysis & conclusion

Saved automatically in this browser.

Post-Lab Test 9 questions · instant hints

A wrong answer shows a hint so you can try again. Only first attempts count towards the score.

9. Numerical: a 3-bit counter steps every 400 ms. How long (in seconds) is one full cycle 0→7→0?

Self-evaluation

Tick what you can now do confidently.

Overall confidence:

Feedback summary

Finish the post-lab test to see your personalised summary.