GOALS Lab 3 ECG System 1) Build and test the signal cnditining electrnics fr an ECG system. 2) Develp the Arduin and MATLAB data acquisitin system. 3) Recrd an ECG wavefrm frm yurself. GENERAL GUIDELINES 1) Due t the limited number f test statins, students can wrk individually r frm teams f tw (nt three). 2) Each student must build his/her wn circuit. 3) Students are encuraged t help each ther. Of curse, Buma will be arund t prvide assistance as well. 4) D nt wrry if yu need lts f help during the lecture and lab activities. Just make sure yu knw yur stuff fr the exams. In additin t the written exams, there will be ne lab practical where each student is tested n basic Arduin and MATLAB skills and putting tgether a cmplete measurement system. 5) Ask questins! The mre questins yu ask, the mre yu learn (assuming Buma can prvide adequate answers ). REQUIRED MATERIALS Lab kit and circuit frm MiniLab3b Oscillscpe, prbe kit, functin generatr, benchtp pwer supply Integrated Circuits (ICs): TL081 p amp (ne) CNY17 ptcupler (ne) Resistrs: 150 hm (brwn/green/brwn) 5% resistr (ne) 220 hm (red/red/brwn) 5% resistr (ne) 1.0 khm (brwn/black/red) 5% resistr (tw) 100 khm (brwn/black/yellw) 5% resistr (tw) 1 Mhm (brwn/black/green) 5% resistr (ne) Capacitrs: 0.1 F capacitr (ne) Red LEDs (three), tw 9V batteries, battery leads (tw), shielded cables (three), and electrdes (three) LAB ACTIVITIES 1) Cnstruct and test input prtectin circuitry, instrumentatin amplifier, and high pass filter 2) Cnstruct and test amplifier + lw pass filter 3) Cnstruct and test the ptcupler 4) Develp Arduin data acquisitin prgram 5) Develp MATLAB data display prgram 6) Make a live ECG measurement. 1
INTRODUCTION The entire ECG amplifier has three main parts: (1) Instrumentatin amplifier with high-pass filter (2) High gain amplifier with lw-pass filter (3) Optcupler. Yu will use the Arduin and MATLAB t recrd and display data. This is the mst time cnsuming lab f the curse. Hwever, seeing yur wn ECG signal with a circuit yu built is a rewarding experience! Inst Amplifier + High-Pass Filter High Gain Amplifier + Lw-Pass Filter Optcupler Arduin MATLAB Battery Pwered PC Pwered Fig. 1: Simplified diagram f ECG system. PART 1: TESTING THE INST AMPLIFIER AND HIGH-PASS FILTER +9V INPUT_1 10.0k + 9 V -9V +9V 2.61k 3 1 7 AD620 (GAIN = 20) 5 6 0.1µF HPF_Output INPUT_2 8 2 4-9 V 1MΩ 10.0k -9V Input Prtectin Instrumentatin Amplifier High Pass Filter Fig. 2: First part f ECG amplifier circuit. During initial testing, +/ 9V and GND are prvided by the benchtp supply. Once everything wrks, the benchtp supply is replaced with tw 9V batteries. Step 1a: Shw Buma yur circuit. He needs t cnfirm that yur dides are crrect and wiring is NEAT. 2
Step 1b: Cnfigure the circuit inputs. Yur circuit will initially be tested with a cardiac wavefrm prduced by the Agilent functin generatr. Build a 100:1 vltage divider (see Fig. 3) fairly clse t the AD620 chip (e.g. within 2 inches r s). The purpse f the divider is t reduce the amplitude f the cardiac test signal t 1 mvpp The resistr clr cdes are n Page 1 f this handut. Use the cax cable with alligatr clips t cnnect the functin generatr t INPUT 1 f the circuit (see Fig. 3). The RED clip ges t the tp f the 100k resistr. The BLACK clip ges t GND. Fr nw, cnnect INPUT 2 t grund (see Fig. 3). If necessary, feel free t take a peek at Buma s bard. Fr nw, we ll use the benchtp supply t prvide +/- 9V and GND t yur circuit bard (see figure t the right). Ask Buma r the lab assistant if yu are unsure abut the pwer supply cnnectins. Later in the lab (after testing is cmplete), yu will replace the benchtp supply with tw 9V batteries. A 0.1 _ V 9.0 + -9V GND A 0.1 _ + V 9.0 +9V Functin Generatr (RED) 100kΩ +9V Functin Generatr (BLACK) 1kΩ 10.0k -9V +9V 2.61k 3 1 + 9 V 7 AD620 (GAIN = 20) 5 6 0.1µF HPF_Output INPUT 1 8 2 4 1MΩ INPUT 2 10.0k -9 V -9V Fig. 3: Fr initial testing, the functin generatr is cnnected t the circuit using a 100:1 vltage divider attached t INPUT 1. Meanwhile, INPUT 2 is cnnected t grund. Fr nw, the +/ 9V pwer cmes frm the benchtp supply. 3
Step 1c: Cnfigure the functin generatr. Yur circuit will initially be tested with a cardiac wavefrm prduced by the Agilent functin generatr. If yu have the lder Agilent functin generatr (Mdel 33120A): 1) Set the Agilent generatr t High Z utput. Press Shift and Enter. Press > three times until D:SYS MENU appears. Press nce t btain 1: OUT TERM. Press again t btain 50 OHM Press > t make High Z appear and press Enter t finish. 2) Set the wavefrm t Cardiac. Press Shift and Arb. Press > fur times t get CARDIAC and press Enter t finish. 3) Set the wavefrm amplitude t 100 mv PP and the frequency t 1 Hz. If yu have the newer Agilent functin generatr (Mdel 33220A): 1) Set the generatr t High Z utput. Press Utility, then select Output Setup, then High Z, and finally select Dne. 2) Set the wavefrm t Cardiac. Press Arb, then select Select Wfm, then Built In, then Cardiac, and finally select Dne. 3) Set the wavefrm amplitude t 100 mv PP and the frequency t 1 Hz. Step 1d: Measure yur circuit utput with the scpe. Set up the scpe in the fllwing manner: Press the Frce buttn if the red Rmt appears in the tp right crner f the scpe display. Reset the scpe by pressing Default n the scpe. Make sure bth the scpe and prbe are set t 1X. Set the scpe t 10 mv/div (vertical) and 200 ms/div (hrizntal). Place the scpe prbe n the utput f the high pass filter. Yu shuld see a PQRST wavefrm with a peak-t-peak amplitude f abut 20 mv. It will prbably appear pretty fuzzy, but that is perfectly OK since the lw-pass filter will clean things up. Dem yur circuit t Buma (10 pts f lab dem grade). 4
(End f Part 1) PART 2: AMPLIFIER WITH LOW-PASS FILTER Step 2a: Build the amplifier with lw-pass filter. This uses a TL081 p amp cnfigured as a nninverting amplifier (see Fig. 4). The TL081 pin diagram is shwn in Fig. 4. Pins 2 and 3 are the p amp inputs. Pin 6 is the p amp utput. Yu d nt need t cnnect anything t Pins 1, 5, r 8. Use clr-cded and neat wiring!!!! Step 2b: Test the circuit. The input fr this circuit cmes frm the high pass filter. Place the scpe prbe n the utput (Pin 6) f the p amp. Change the scpe vertical setting t abut 1 V/div. Yu shuld see a much larger PQRST wavefrm with abut a 2V peak-t-peak amplitude. The amplified signal shuld be MUCH CLEANER than what yu bserved in Step 1d. This shws the imprtance f lw-pass filtering! Dem yur circuit t Buma (10 pts f lab dem grade). Fig. 4: Amplifier with lw pass filter using a TL081 p amp pwered by +/ 9V. The pin diagram is shwn n the lwer right. (End f Part 2) 5
PART 3: OPTOCOUPLER As mentined previusly, the ptcupler transmits and receives light with an LED and phttransistr, respectively. Our ptcupler is the CNY17 chip. Step 3a: Hk up the left side f the ptcupler. This is the battery side f the ptcupler (althugh we are currently using the benchtp supply t prvide +/- 9V fr testing). The left side f the CNY17 chip has a slight bevel (see Fig. 5). The three red LEDs utside f the chip are handy indicatrs f the ECG wavefrm it pulsates with yur heart rate! The lnger LED pin is the tp side f the LED. The bias current in the ptcupler LED is rughly 10 ma, which is determined by the red LEDs and 220 hm resistr. Use clr-cded and neat wiring!!!! Fig. 5: Optcupler circuit, where the left side is pwered by +/ 9V. The right side is SEPARATELY pwered by the Arduin. Step 3b: Hk up the right side f the ptcupler. This is the PC side f the ptcupler. Cnnect Pin 5 f the ptcupler t the +5V pin f the Arduin. Cnnect Pin 4 f the ptcupler t a 150 hm resistr that leads t GND frm the Arduin. D NOT cnnect the Arduin GND t the rest f the breadbard. Remember that we want separate battery grund and PC grund! Use clr-cded and neat wiring!!!! 6
Step 3c: Test the circuit. Make sure the Arduin is plugged int the cmputer (via USB cable). The red LEDs shuld have a peridic (and faint) flickering at a 1 Hz rate. This is because the LED current is mdulated by the ECG signal! The flickering will be fairly subtle, s dn t expect dramatic flashing frm the LEDs. Attach a scpe prbe t the ptcupler utput (Pin 4). The prbe grund shuld be the same as the Arduin. Yu shuld see the ECG wavefrm with 1 vlt amplitude and a 2V ffset. Dem yur circuit t Buma (10 pts f lab dem grade). OK, yu re dne with yur circuit! Nw fr Arduin and MATLAB prgramming (End f Part 3) 7
PART 4: ARDUINO In the previus lab, each data sample required a separate request frm the PC t the Arduin. This back-and-frth cmmunicatin is fine fr slw sampling rates, but is prblematic fr applicatins requiring high sampling rate. ECG typically requires a sampling rate f 100 Hz r higher. This is much t fast fr ur ld methd f data acquisitin. We will instead have the Arduin send data t the PC in bursts. An utline fr the new cde is shwn belw: (a) Setup( ) rutine (b) Lp( ) rutine Initialize the serial prt Send message t PC Wait fr message frm PC If PC requests data, Repeat the fllwing fr N = 300 times: Recrd ADC utput Cmpute Vmeas Send Vmeas t cmputer Wait 10 ms STEP 4a: Dwnlad and unzip the Arduin template. G t the curse website and dwnlad Lab3_files.zip t yur desktp. Duble-click n the zipped flder. Select Extract all files (near tp f the windw) t unzip the cntents f the flder. Duble-click the Lab3_Arduin flder and pen the template. Yu can cnsult the Lab1+2 handuts r Ggle fr help and/r examples using Arduin functins. STEP 4b: Write the setup( ) rutine. Initialize the serial prt using the Serial.begin cmmand Replace the??? with apprpriate cde. Use the Serial.begin cmmand and set the data rate t 57600 bits per secnd. Dn t frget the semicln at the end f each line! Send message t PC Use the Serial.println cmmand t send the character a. 8
STEP 4c: Declare variables fr the lp( ) rutine. The lp rutine uses five variables. Declare these befre the lp( ) rutine. As shwn in Fig. 6, Buma wrte the first ne fr yu (he is s kind.). Replace any ther??? with apprpriate cde. Data Type Name Value Purpse char PCstatus Stres ne byte f text data frm the PC int A0_PIN 0 Name f Analg Pin 0 int ADCutput Stres the ADC utput flat Vmeas Measured vltage int T 10 Time between readings (ms) int N 300 Number f samples per trace Dn t frget semiclns at the end f each line! Fig. 6: Declare sme variables befre the lp( ) rutine. STEP 4d: Write the lp( ) rutine. Replace all the??? s that yur cde lks like Fig. 7. Cmments abut the cde. The Serial.available cmmand checks if the serial prt has received any data. The while lp repeats this prcess and stps when data has been received. The Serial.read cmmand reads ne byte f received data frm the serial prt. The Serial.read cmmand reads ne byte f received data and stres the value in PCstatus. If PCstatus is the character y, then we prceed t recrd/send an entire data trace (N = 300 samples). We use a fr lp t repeatedly acquire each sample until a full trace is finished. The variable n keeps track f the lp iteratin. Use the analgread cmmand t read the data frm A0_PIN. 9
Cnvert ADCutput t Vmeas (see Lab1 handut if yu frgt hw t d this). Serial.println(Vmeas, 3) sends the Vmeas value (3 decimal places) Use the delay cmmand t wait T secnds befre the next sample acquisitin. Fig. 7: The lp( ) rutine checks fr a message frm the PC and then makes a vltage measurement. STEP 4e: Uplad yur cde and bserve the Arduin utput using the Serial Mnitr n the cmputer. Make sure the bttm right f the windw is set t 57600 baud. Yu shuld ntice the letter a n the first line. Type in the letter y in the cmmand line and press the Send buttn. Yu shuld see a rapid burst f vltage values appear (see Fig. 8). Every time yu send y, a new burst f data shuld appear. If yur cde wrks, CLOSE the Arduin sftware. Fig. 8: Sending 'y' t the Arduin shuld result in a rapid burst f data. (End f Part 4) 10
PART 5: MATLAB OK, nw we need t write a MATLAB prgram t receive data frm the Arduin and plt the ECG signal. In the Lab3_files flder yu recently dwnladed frm the curse website, duble-click n the Lab3_MATLAB.m file. STEP 5.1a: Replace any??? in the template s that yur cde lks like Fig. 9. Cmments abut the cde are shwn belw: Creating the serial prt cnnectin (use the same COM prt number as the Arduin cde!): We are using the set cmmand t cnfigure the serobj data rate fr 57600 bits per secnd. We are recrding N = 300 samples fr each trace. The burst f data frm the Arduin is stred in a memry buffer. We are setting the input buffer size f serobj t be 8*N bytes. Waiting fr the Arduin message. This is the same as befre. Fig. 9: Yur MATLABcde shuld lk like this. D NOT frget the semiclns! STEP 5.1b: Replace any??? in the template s that yur cde lks like Fig. 10. Cmments abut the cde are shwn belw: Initialize the plt windw We first need t create a 1-D array f time values fr pltting. The time vectr t has a length f N = 300 samples. The time increment is dt = 0.01 secnds. 11
The time vectr is therefre [0, dt, 2dt, 3dt,..., 299dt] = [0, 1, 2, 3,..., N-1]dt We next create an array f vltage values. The vltage vectr Vdata will initially be defined as a 1-D array f nes. The figure cmmand creates a windw, where we assign h as the figure name. The plt cmmand plts t n the x-axis and Vdata n the y-axis. drawnw displays the plt immediately (this is necessary while the prgram is running). We want the prgram t STOP when the user clicks n the figure windw. We can d this using the ButtnDwnFcn in MATLAB. We use the set cmmand t cnfigure the ButtnDwnFcn t d whatever we want. We define a variable q that is initially equal t 0. When the use clicks n figure h, we want ButtnDwnFcn t make q = 1. Later in the cde we ll see hw q = 1 causes a while lp t exit. Fig. 10: This part f the cde creates 1 D arrays fr time and vltage, displays a plt, and cnfigures the ButtnDwnFcn feature. STEP 5.1c: Nw we want the Arduin t send a cmplete data trace t the PC! Replace all??? with apprpriate cde shwn in Fig. 11. Cmments abut the cde are shwn belw: The while lp keeps running as lng as q is equal t 0. Request the data samples frm the Arduin by sending the letter y. Nw we need t WAIT fr all the data t be acquired by the Arduin and sent t the PC. The while lp runs until there is enugh data (7*N bytes) in the serobj memry buffer. This is dne with serobj.bytesavailable < 7*N. We nw run a fr lp t read the received data. Each array element in Vdata is updated with a received flating pint data value. 12
Pltting the updated data is the same as befre. The last sectin f the prgram ( Finish up the prcess ) is the same as befre. Fig. 11: The data acquisitin lp des tw things: (1) receives data trace frm the Arduin (2) displays an updated plt. STEP 5.1d: Save yur prgram, then run it by clicking n the Run buttn (with green triangle) at the tp f the MATLAB windw. An initial plt shuld appear shwing a flat line with amplitude = 1. After a few secnds, a new plt shuld appear shwing yur amplified ECG signal. If yu get MATLAB errrs, remember t type fclse(serobj) in the MATLAB cmmand line BEFORE debugging yur cde! (End f Part 5) 13
PART 6: ECG MEASUREMENT STEP 6.1: Discnnect the benchtp supply frm yur breadbard. Insert tw battery cnnectrs t the breadbard (see Fig. 12). Fr +9V pwer: +9V = RED wire Gnd = BLK wire Fr the -9V pwer: -9V = BLK wire Gnd = RED wire Attach a 9V battery t each cnnectr. Run yur MATLAB cde t make sure everything wrks. Fig. 12: An example f hw t attach the battery cnnectrs t yur breadbard. This schematic assumes 9V and GND are n the left vertical strip while +9V and GND are n the right vertical strip. Fig. 13: ECG measurement using shielded cables and electrdes (nly first part f ECG circuit is shwn). STEP 6.2: OK! We are nw ready t d a live ECG recrding! Remve the 100:1 vltage divider frm yur bard. Ungrund INPUT 2 f yur bard. 14
Buma will give yu three shielded cables and electrdes. Attach them accrding t Fig. 13. Firmly press each electrde t yur skin. Pr electrde-skin cntact will degrade the ECG measurement! Red clip ges t upper left arm. The cable s yellw wire ges t INPUT 1 f yur bard. Black clip ges t upper right arm. The cable s yellw wire ges t INPUT 2 f yur bard. Black/Green clip ges t lwer right arm. The cable s yellw wire ges t BATTERY GND f yur bard. The green wires f all three cables are cnnected t BATTERY GROUND! Be very still during the ECG recrding -- hpefully yu will see yur PQRST wavefrm n the cmputer! Recrd a snapsht f yur ECG wavefrm fr yur lab reprt. If yu want t adjust the y-limit values f yur plt t better display the wavefrm, yu shuld STOP the data acquisitin befre editing the cde. Take a pht (e.g. with camera phne) f yur wrking circuit fr yur lab reprt. Dem yur system t Buma. Fig. 14: Buma's ECG recrding. If yu want t adjust the y limit values f yur plt, yu shuld STOP the data acquisitin befre editing the cde. (End f Lab3) 15