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
Explain why relays and buzzers need a transistor/ULN2003 driver and a flyback diode.
Control single pins with sbit declarations.
Combine two input bits into a mode number and dispatch with switch-case.
Choose delays suited to mechanical relays (≥ 10 ms) and audible buzzers.
Recognise the hazard of mains-voltage loads on the relay contacts.
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
Software delay in C
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
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)
| Device | Pin | Logic |
|---|---|---|
| SW0, SW1 | P3.0, P3.1 | ON = 0 |
| Buzzer | P2.0 | 1 = ON (via transistor) |
| Relay | P2.1 | 1 = energised (via ULN2003) |
| LEDs | P1.0–P1.7 | 1 = ON |
Apparatus & environment
| Item | Specification | Qty |
|---|---|---|
| PC with Keil µVision | C51/A51 tool chain (evaluation version is fine) | 1 |
| 8051 trainer board | Nuvoton W78E052D, 11.0592 MHz crystal | 1 |
| Nuvoton ISP-ICP utility | APROM programming over USB-UART (CH340) | — |
| USB cable | USB-A to the board's serial/ISP port | 1 |
| Power supply | +5 V regulated (board adapter) | 1 |
| Relay module | 5 V SPDT relay with ULN2003/BC547 driver | 1 |
| Buzzer module | 5 V active buzzer with transistor | 1 |
| Switch & LED modules | 2 switches, 8 LEDs | 1 |
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)
- 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.
- Build and program 8.1 (relay). Listen for the click and watch the relay LED.
- Build and program 8.2 (buzzer).
- Build and program 8.3. Set each of the four switch combinations and record the behaviour in the table.
- Measure the relay ON time and the counter step time.
Doing it in this virtual lab
- Choose a sub-task chip. The program opens in the µVision window.
- Press 🔨 Build (F7). Errors are listed in Build Output: click one to jump to the line.
- Press ▶ Run (F5) and watch the board. Use ¼× to 1/1000× slow motion to see fast events.
- Use Step (F11) and Step Over (F10), and click line numbers to set breakpoints.
- Edit the code (delay, pattern, pins), rebuild, and observe the change. Press ● Record to log results.
- 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-task | Inputs | Expected | Simulator | Your value | Check | Note |
|---|
Edge cases to test
- 8.1: delay_ms(2): watch the relay chatter warning.
- 8.3: remove one
break;and see the next case also run. - 8.3: delete
SW0 = 1; SW1 = 1;after writing 0 to them: the switches can no longer be read. - 8.3: change a case to
case 4:: can it ever be reached? - 8.2: drive the buzzer at 1/100× speed: notice the timing of each beep.
Analysis & conclusion
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.
Self-evaluation
Tick what you can now do confidently.