Breadboard and Arduino ECG
In this Instructable, we will explain how to create your very own ECG on a breadboard by connecting three simple circuits: an instrumentation amplifier, a notch filter, and a low pass filter. By the end, you will be able to visualize an ECG signal on an oscilloscope and measure the heartrate using Arduino.
Supplies
- Breadboard
- LTSpice Software
- Two 9-Volt Batteries
- Five Operation Amplifiers
- Assortment of Jumper Wires
- Three ECG Leads and Patches
- Seven Sets of Alligator Clips
- Two 1.6 kOhm Resistors
- Three 2 kOhm Resistors
- Two 18 kOhm Resistors
- Two 22 kOhm Resistors
- Two 100 kOhm Resistors
- One 390 kOhm Resistor
- One 15 nF Capacitor
- One 0.033 uF Capacitor
- Two 0.1 uF Capacitors
- One 0.22 uF Capacitor
- Oscilliscope
- Function Generator
- Two BNC to Alligator Connectors
- Computer
- Arduino
- LED Light (Optional)
Instrumentation Amplifier Simulation
Creating a simulation for each component of the circuit is important for visualization of how the components are pieced together and as reassurance that the circuit should work in theory. For each circuit component in this ECG, the schematics will be drawn and simulated in LTSpice.
The first component in the circuit is the Instrumentation Amplifier. This component is designed to accept a small input voltage from the heart and greatly amplify it for better visualization. We chose our resistor values specifically to achieve a gain of 1000, which amplifies the system enough to easily be read on an oscilloscope, but isn't so high that it exceeds the 9V power source from the batteries.
Notch Filter Simulation
The next component in the circuit is the Notch Filter. This component is designed to filter out any noise that comes from power lines. This noise occurs at an approximate frequency of 60 Hz, so the resistor and capacitor values here were calculated to attenuate frequencies close to 60 Hz. Any frequency lower or higher than this value will be preserved.
Low-Pass Filter Simulation
The final component in this circuit is the Low Pass Filter. This component is designed to only read frequencies that correspond to normal frequencies from the heart. This ECG is being tailored to adolescents and adults, whose readings are typically composed only of values below 150 Hz. For this reason, this low pass filter utilizes resistor and capacitor values that achieve a cutoff frequency of 150 Hz, attenuating any signal with a higher frequency.
Make Circuit Components
Now it's time to start building the ECG. Each component will be completed separately on the breadboard so that they can be tested individually with a function generator to ensure that each component is working properly. Using the models and simulations from earlier, piece together each respective component.
For the Instrumentation Amplifier:
- Play around with the resistor values if the readings are not being displayed as expected. If the readings are not stable, try changing resistor values to bring the gains of the first and second stages closer together.
For the Notch Filter:
- We did not possess all of the components used in our simulation, so we used R2 = 390 kOhms and C3 = 0.22 uF instead of what is listed in the schematic. These components still provided results similar to our expectations.
For the Low Pass Filter:
- We did not possess all of the components used in our simulation, so we used R1 = 22 kOhms, R2 = 22 kOhms, and C1 = 15 nF instead of what is listed in the schematic. These components provided a circuit whose cutoff frequency is still close to 150 Hz, which makes it still serve its purpose.
When you know each component works, connect them in series so that the instrumentation amplifier accepts the inputs and the low pass filter is the final output.
Display the ECG
Take three patches and place one on each ankle and another on the right wrist. Next, connect ECG leads to each of these patches. Using alligator clips, connect the lead on the right ankle to the ground of the circuit, the lead on the left ankle to the first (positive) input, and the lead on the right wrist to the second (negative) input. If the circuit is built correctly and hooked up to an oscilloscope, the ECG waveform should be visible on the screen.
If the positive and negative leads are switched, you will get an ECG that is flipped upside down as shown above. Simply reverse the leads to correctly orient the waveform.
Display on Arduino
The display on the oscilloscope can easily be replicated on a computer through the use of an Arduino. To accomplish this, first copy the code shown below into Arduino. Next, connect the output of the ECG to pin A0 on the Arduino and make a ground connection. After attaching electrodes using the lead placement described above, the serial plotter tool can be used to display the ECG signal in real time.
int UpperThreshold = 700; //Thresholding of where to read "beats" based on your signal input
int LowerThreshold = 600;
int reading = 0;
int BPM = 0;
bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;
//added for EC
const int heartRate = A0;
const int led = 9;
int val;
void setup(){
Serial.begin(74880);
}
void loop(){
reading = analogRead(A0);
// Heart beat leading edge detected.
if(reading > UpperThreshold && IgnoreReading == false){
if(FirstPulseDetected == false){
FirstPulseTime = millis();
FirstPulseDetected = true;
}
else{
SecondPulseTime = millis();
PulseInterval = SecondPulseTime - FirstPulseTime;
FirstPulseTime = SecondPulseTime;
}
IgnoreReading = true;
}
// Heart beat trailing edge detected.
if(reading < LowerThreshold && reading > 2){
IgnoreReading = false;
}
BPM = (1.0/PulseInterval) * 60.0 * 1000;
//Serial.println(A0);
Serial.print("BPM = ");
Serial.println(BPM);
delayMicroseconds(3900);
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue;{
// read the input on analog pin 0:
//int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
// print out the value you read:
Serial.println(voltage);
};
// print out the value you read:
Serial.println(voltage);
}
}
Finding Heart Rate
The display shown above from the serial plotter tool has clear peaks from a QRS complex. This can be used as an indicator for a single heart beat. The QRS complex should consistently cross a threshold that no other part of the wave crosses. To calculate the heart rate, assign an upper and lower threshold in the code. When the serial monitor tool is used, the Arduino can calculate how many times the QRS complex occurs within a minute, which is equivalent to the heart rate.
LED Indicator of QRS Complex
An optional addition to the ECG circuit is an LED light that is activated throughout the duration of the QRS complex. In order to accomplish this, implement the additional lines of code shown below in the Arduino and connect an LED light in parallel with the output of the circuit.
int UpperThreshold = 700; //Thresholding of where to read "beats" based on your signal input
int LowerThreshold = 600;
int reading = 0;
int BPM = 0;
bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;
void setup(){
Serial.begin(74880);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop(){
reading = analogRead(A0);
// Heart beat leading edge detected.
if(reading > UpperThreshold && IgnoreReading == false){
digitalWrite(LED_BUILTIN, HIGH); // heart beat reaches the threshold value of the QRS complex and LED turns on
if(FirstPulseDetected == false){
FirstPulseTime = millis();
FirstPulseDetected = true;
}
else{
SecondPulseTime = millis();
PulseInterval = SecondPulseTime - FirstPulseTime;
FirstPulseTime = SecondPulseTime;
}
IgnoreReading = true;
}
// Heart beat trailing edge detected.
if(reading < LowerThreshold && reading > 2){
digitalWrite(LED_BUILTIN, LOW); // heart rate doesn't reach the threshold value of the QRS complex, LED turns off
IgnoreReading = false;
}
BPM = (1.0/PulseInterval) * 60.0 * 1000;
//Serial.println(A0);
Serial.print("BPM = ");
Serial.println(BPM);
delayMicroseconds(3900);
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue;{
// read the input on analog pin 0:
//int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
// print out the value you read:
Serial.println(voltage);
};
// print out the value you read:
Serial.println(voltage);
}
}