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.