Editorial Solution

From the task, we understand that,

  • We have to detect the pulse of width 64 nanoseconds.
  • The code for Pulse generation is already given.
  • The LED is toggled once the pulse is detected.
  • To achieve this we have 2 Approaches:
    • By continuously polling in a void loop().
    • Using Interrupt.

Detecting the pulse using polling

  • digitalRead() takes about 4 µs.
  • Pulse duration is around 64 ns.
  • Detecting such a short pulse with digitalRead() is not possible.

Detecting the pulse using an interrupt 

  • Time consumed by Interrupt
  • State change interrupt: 250-312 ns (4-5 cycles)
  • Edge interrupt: 62.5 ns (1 cycle)
  • Rising or falling edge interrupt (INT0, pin 2) must be used for detection.

 

Hardware Connection

  • We have two Arduino UNO boards for
    • Pulse Generator
    • Pulse Detector
  • Pulse generator (pin 7) → Pulse detector (pin 2)
  • We will use an onboard LED connected to pin no. 13 of Arduino UNO.
  • Make GND common.

 

Circuit Connection

 

Firmware

The firmware 

  • Set external interrupt on pin 2 for the rising edge.
  • Toggle the onboard LED on pin 13 when the interrupt occurs.

 

Code


// Interrupt Service Routine for INT0 (pin 2)
void ISR_INT0() {
  digitalWrite(13,!digitalRead(13));      //toggles the LED when pulse detected
}

void setup() {
  pinMode(2, INPUT);    // Configure interrupt INT0 (pin 2) as input
  pinMode(13, OUTPUT);  //onboard LED is connected to pin 13

  // Attach external interrupts
  attachInterrupt(digitalPinToInterrupt(2), ISR_INT0, RISING); // Trigger on rising edge for INT0
}

void loop() {

}


Code Explanation

This code uses an external interrupt (INT0) on pin 2 of an Arduino to detect a rising edge signal and toggle an LED on pin 13.

Pin Setup

  • pinMode(2, INPUT): Configures pin 2 as an input.
  • pinMode(13, OUTPUT): Configures pin 13 as an output to control the onboard LED.

Interrupt Attachment:

  • attachInterrupt(digitalPinToInterrupt(2), ISR_INT0, RISING): Sets up an interrupt on pin 2 to trigger on a rising edge.

ISR Functionality:

  • When a rising edge is detected, ISR_INT0() is executed:
    • Toggles the LED on pin 13 using digitalWrite(13, !digitalRead(13)).

Main Loop

  • The loop() function is empty since the program is interrupt-driven and responds instantly to external events.

 

Output

Hardware Setup

 

Video


 

Submit Your Solution