Sunday, March 24, 2013

Interfacing with MATLAB

We can use MATLAB to talk to the instruments in lab via an UART (Universal Asynchronous Receiver/Transmitter). We used the RS-232 serial communications protocol because of its simplicity. Only two lines are required, connecting the tx and rx lines of the two devices. One byte can be transmitted at a time. RS-232 is idle high (i.e. it transmits high voltage when no data is being sent) and active high (i.e. high voltage is a logical one). When a byte is transmitted, the start bit, a logical zero is sent, followed by the byte to be transmitted, and then the stop bit, a logical one. Each bit lasts for a predetermined amount of time, the bit time, which both devices must be aware of. (In reality, the baud rate, which is the inverse of the bit time, is more commonly used.)

We can set up the LogoChip to use serial communication by running the serialcomm.pb PicoBlocks program from the website. Two output pins of the LogoChip are now rx2 and tx2 lines for use in serial communication. (The rx2 is also connected to +4V via a 10 kOhm pullup resistor, while the tx2 does not need to be connected to anything else.) Note that only one set of rx and tx lines may be in use at the same time!!!

We can send the following numbers to the LogoChip (via PicoBlocks) and observe the resulting transmitted byte on an oscilloscope



10 

39 

100 

157 
Pictures taken by Hannah

Since the bit time was  52 microseconds, the baud rate is 19200

We can vary the baud rate by varying the init block in PicoBlocks. In particular, we can change the value of X in the line "write $238 X". From the actual tech specs, we know that the baud rate is 1/(X+1). 

The LogoChip records 10 bit sensor values, so we need to break the number into a low byte and high byte number. The low byte has the 8 LSB, and the high byte has the 2 MSB (and some 0s). 

The PicoBlocks program to implement this is pretty short

We also wanted to have the LogoChip transmit light sensor data at 10 Hz. Every 100 ms, it transmits the low, then high bytes.

Note that we will be using two's complement to transmit negative numbers.

We can use MATLAB to collect and graph the light sensor data! The LogoChip will then turn on the LED depending on how much light is detected by the sensor. Here is the MATLAB code. (Note that I am linking to Hannah's site here, and for the other MATLAB codes)


Video by Hannah

We then use a USB-GPIB interface to connect a function generator to our computer, using the MATLAB code here.

We can play lots of things, including Mary Had a Little Lamb! (MATLAB code for Mary Had a Little Lamb here; code for playing an octave here.)

Video by Hannah

Thursday, March 7, 2013

Day 8 and 9: More OpAmp circuits

So what can we do with OpAmps?

We can make OpAmp followers

The Vout is perfectly the same as Vin! This is a case of the noninverting amplifier.
Zin is still large and Zout is still small.

We can also make current sources

Where DVM is the ammeter.
We expected a current of a few mA and got 3.8 mA.
Once the max voltage is reached, the current can no longer be regulated.
The extra 10 kOhm resistor is needed to increase the max voltage. Without it, the max voltage is a mere 3/4 V, leaving the current unregulated.

We can make a current to voltage converter

The LED is used instead of an LPT100, but it behaves the same way, converting light into current.


Pictures by Hannah

The 100 pF capacitor in parallel with the 10 MOhm resistor reduces fuzz in output. It gets rid of the high frequency oscillations by increasing the impedance, giving low frequencies more gain. (Gain being the current to voltage conversion in this case.) This is a standard strategy since OpAmps generally don't behave well with high frequencies.

Note that we see a signal oscillating at 60 Hz, as expected!

The average DC output is 125 mV.
Percent modulation is AC Vpp / DC = 40 mV/125 mV = 30%
This output level corresponds to an input photocurrent of (using DC only)
V=IR
125 mV = I * 10 MOhm
I = 1.25E-9 A
Recall that we are using an LED. With a real photodiode, we'd get VDC closer to 10 V

This circuit is preferable to a circuit without the OpAmp, since this circuit also amplifies the voltage.

The summing junction is a virtual ground, and we do see 0V at the summing junction experimentally!

We can also use a phototransistor
Second picture by Hannah

VDC = 4V
I = VDC/R = 4 V / 100 kOhm = 4E-5 A
AC Vpp = 4V
Percent modulation is 100%
Summing junction is again virtual ground.

We can make a summing amplifier



Photo and video by Hannah

This sums a DC level with the input, basically adding a DC offset
In the video, Vin is yellow and Vout is blue. As the pot is turned, the DC offset changes and Vout moves up and down. The offset is between +/- 8 V as expected. The Vout is inverted, but the amplitude of the wave is the same.

As shown in class, the following circuit is subtract two voltages

However, there are some limitations of the OpAmps!
The first being the slew rate.

Using a 1 kHz square wave, varying the Vpp, the slope of Vout at the transitions in the square wave will determine the slew rate. Note that the slew up and down might be different!







Vpp (V) Voltage change (V) Time up (ns) Slew up (V/s) Time down (ns) Slew down (V/s) Picture
1 1 200 5E6 200 5E6
3 3.2 320 1.1E7 300 1.3E7
5 5.4 450 1.2E7 400 1.4E7
7 7.2 650 1.1E7 530 1.4E7

With a sine wave, we now want to see when Vout begins to drop appreciably.

Picture by Hannah
From 1 MHz to 2 MHz, we observe a huge drop. In the picture above, the 1 kHz Vmax is lined up with the grid line. We can see that at 1.5 MHz, the Vmax has dropped quite a bit.
This makes sense. The slew is around 1 V/ 10 µs  so when the period of the wave is larger than 10 microseconds and the frequency is around 1 MHz, the OpAmp starts having troubles.

Now we can make an integrator with the OpAmp

Driving with a 1 kHz 1 Vpp square wave...
Picture by Hannah
Note that Vout = -1/(RC) ∫ Vin dt
R = 100 kOhm and C= 0.01 µF
∫ Vin dt = 0.5 V * 500 µs (Considering only half the period)
So Vout = -250 mV

Our experimental Vout is also -250 mV!

Note that Vout is also offset because of the small DC offset. The gain for DC is 100, so a small offset in Vin means a huge offset in Vout.

Picture by Hannah
Now with a Vin of 2 Vpp and 500 Hz, we get a Vout both experimentally and calculated of -1V

You may be wondering why there is a 10 MOhm resistor in the circuit. It is preventing the DC offset from entering the integral. The DC current goes through this bypass resistor instead of building on the capacitor. If the resistor were not there, the integral would drift to a rail.

Now a microphone amplifier!

Turning the pot changes the amplitude of Vout. The microphone is also capable of correctly identifying the frequency of the sound entering the microphone! (The concert A of 440 Hz was generated by Audacity.)

The video is not uploading correctly here, but it is up on Hannah's blog.

Friday, March 1, 2013

Day 7: Amplifiers!

OpAmps are very cool and we can make some useful circuits with them!

First, an open loop test circuit

When the pot is turned, the Vout should go from -12V to +12V basically instantaneously.

Picture by Hannah

The max gain cannot be realized, since the max Vout is (+/-) 12 V. So we cannot verify the gain, but we can show that it is consistent with a gain >> Vout, since the change in Vout is instantaneous.

Now inverting amplifiers

Second picture by Hannah

By driving with a Vin of 1 Vpp and 1 kHz sine wave (not pictured), we get a Vout of 5 Vp.
G=5/0.5 = 10. (Recall that V peak to peak is 2 * V peak)
This is consistent with our prediction of R2/R1 = 10
The out put swing is Vpp = 10 V.

Something cool is going to the rail: this is when you get the max output from the opamp, so the sine wave has cut off tops and bottoms.
Picture by Hannah
 The maximum output of the swing when it is at the rails is 22 V (2 less than the input into the amplifier).

A circuit is linear if Vout is in the same shape as Vin (ignoring the 180 degree phase shift). Since the output of the amplifier is in the same shape, it is therefore linear.

At high frequency the amplitude of Vout actually decreases! So it acts as a low pass filter.

With a 1 kOhm resistor in series with Vin, as above, we can see that Vin = 2 Vpoint, indicating that the input impedance, Rth, is the same as the 1 kOhm resistor.

Similarly, when a resistor is in series with Vout, we see that the output impedance must be very small compared to 1 kOhm, since Vpoint is the same as Vout.

Now for the non inverting amplifier!

With a 1 Vpp 1 kHz sine wave, we get a Vout of 5.5 V that is in phase with Vin.
This indicates a gain of 11, which is consistent with our prediction of 1+R2/R1.

Our input impedance can be determined in a similar way to above:
With a DC input, Vpoint = Vin.
This indicates Rth >> 1 MOhm

Using an AC input, we expect the capacitor's impedance to dominate. (It will be much less than the resistor's very high impedance.)
The frequency is 1 kHz
Vpoint = 1/2 Vin, so impedance is 1 MOhm
Impedance = 1 /(frequency * capacitance)
Capacitance = 1/(frequency * impedance) = 1000 pF

The output impedance should still be really small.

Tuesday, February 26, 2013

Day 6: Diodes and More Capacitors

In order to add a DC offset to any Vin, we can simply use a blocking capacitor and build this circuit

Second picture by Hannah

This particular circuit causes a 4 V offset since we are using a 12 V power supply. 



Picture by Hannah

However, at low frequencies, the offset becomes much smaller. In the picture above, the frequency is very high -- 100 kHz. At a lower frequency, like 1 Hz, we merely observe



Picture by Hannah

The offset is still 4V, but because of the similarities of the circuit to a high pass filter, the low frequency wave has been attenuated!





Second picture by Hannah

We can also bring the offset back to 0 V with the circuit above. At 100 kHz, the oscilloscope reads a Vout that looks exactly like the Vin coming out of the function generator. At lower frequencies like 1 Hz, the magnitude of Vout is decreased. So... basically yet another iteration of the high pass filter!


We can figure out the low frequency limit by measuring 3dB. This is, if you recall, the point at which Vout is 70.7% of Vin. So since Vout goes from 1500 mV at 1 MHz to 1050 mV at 11 Hz, the low frequency limit is somewhere in the 11 Hz range.


The next section is on diodes! 

An ohmmeter measures 0.605 V as the forward bias of the diode, and ~5 Mohm as the resistance (though the latter is largely not useful unless we measure a lot of diodes with the same ohmmeter). And of course, we get an infinite voltage if we try to measure the reverse bias.

Now we can build a half wave rectifier!

Second picture by Hannah

We used a variable transformer set to 6 Vac. 

Recall that Vpp = 2 sqrt(2) * Vrms
So, to get a Vrms of 6 V, the power supply should be adjusted until Vpp = 16.8 V

Alternatively, our oscilloscopes will automatically calculate the Vrms and Vpp of a wave.

Picture by Hannah
Yellow is Vin; Blue is Vout

And it lets only positive voltage through, as expected! Of course, we get a Vp > 6 V, since Vp > Vrms


The next task is to build a ripple. 

A 47 μF capacitor is added in parallel with the 2.2 kΩ resistor in the half wave rectifier circuit, being careful to keep the negative end of the capacitor towards ground.


Picture by Hannah
Yellow is Vin; Blue is Vout


As seen above, the measured ΔV = 900 mV

We can calculate ΔV as follows:

ΔV = Vmax (discharge time / (Rload C))
       = 8.4 V (14.8E-3 s / (2.2E3Ω 47E-6 F))
       = 1.2 V
We had to measure discharge time of the capacitor, since it is obviously not discharging for the entire period. Another adjustment could have been measuring the actual Vmax, since Vmax of the output is not the same as that of the input.

Recall that a capacitor has 20% error, so our result has an error of approximately the same order.

Next, we can build a signal diode (aka rectified differentiator)
Second picture by Hannah

By driving the system with a 10 kHz square wave at 20 Vpp (the max our function generator can generate), we ge the following:


Picture by Hannah
Blue shows Vin; Yellow shows Vout

Only the positive derivative is produced!


Without the 2.2 kΩ resistor, the circuit is a mere RC circuit. With the oscilloscope's resistance being 1 MΩ, the time constant is extremely large, so the discharge time is much slower. The 2.2 kΩ resistor therefore draws current through itself, allowing the derivative to correctly be a spike with a short discharge. 


Next, a diode limiter!

Second picture by Hannah

A diode limiter is a wonderful creation that cuts off high voltage. For the following images, we are using a Vin of 100 Hz at the max amplitude of 20 V. 






Pictures by Hannah
Blue is Vin; Yellow is Vout

Since 20 V is much larger than the cut off, we can see clearly how the diode limiter cuts off high voltages.


Picture by Hannah
Blue is Vin; Yellow is Vout

However, the above image has a 100 Hz frequency but only a 1 V amplitude. It does not get cut off very much! This would be very useful if we had an instrument that was damaged by high voltage.

Now we will consider the impedances of test instruments! While we'd like our measurements to not affect the system at all, of course our instruments are not perfect. 


By driving our oscilloscope with a 100 Hz sine wave and the following circuit


We can see that at the attenuation is approximately 1/2.

At the higher frequency of 10 kHz, the attenuation is 1/4.
So, as frequency increases, Vout decreases.

The oscilloscope behaves as a low pass filter!


So, at low frequencies, we should observe the circuit behaving as a simple voltage divider. Since the attenuation was 1/2, we know the resistance of the oscilloscope must be approximately 1 MΩ. This also means the capacitance will be small, somewhere in the 10s of pF.

Recall that the magnitude of the impedance of the capacitor will be 1/ωC. 
At a higher frequency of 10 kHz, the attenuation is 1/4
We can draw the two resistors as one resistor with Rth = 0.5 MΩ.
Now, we can say that for an RC circuit,
Vout = Vin /(1+iωRC)
attenuation = 1/(1+ωRC)
C = (4 - 1) / ωR = 95 pF, using a ω of 2 Pi 10 kHz and R of 0.5 MΩ.

The coaxial cable also has capacitance which makes this capacitance higher than the capacitance of the oscilloscope.

To make the circuit a divide by two attenuator, we must decrease the capacitance of the oscilloscope. This will make the resistance of the scope will dominate even at higher frequencies and still behaves as a voltage divider.

Probes are particularly useful because they increase the impedance.
With a 10x probe where Vout is above, Vout ~ Vin.