Brownian Motion Javascript Simulations

Posted January 27th, 2014. Filed under Labs Physics Labs

In order to run the following simulations you need a web browser capable of running javascript and of interpreting HTML5.

Brownian Particle Simulation in 2D

A Simulated Time-Lapse Video of Brownian Particles in 2D

Making a PDF of a Microsoft Word Document

Posted January 14th, 2014. Filed under Guides Microsoft Office

Mac OS X

Select File > Print …
Click PDF > Save as PDF …
Specify Save Location and Click Save

Windows

Click the Office Button
Select Save As
Select PDF or XPS
Select Save Location and Save as Type > PDF and Click Publish

If you are working on a project that responds to sounds, your circuit will require some type of microphone to transduce the sound into a modulated voltage and, most likely, some type of amplification of this modulated voltage. I like to use electret microphones to transduce sound because they require relatively simple circuits. For example, the circuit below passes the output of an electret microphone (hooked up according to its datasheet) through a band-pass amplifier based on an LM358 operational amplifier. This circuit is suitable for a 5-V system. The op-amp circuit has both a high-pass portion (R1 and C1) and a low-pass portion (R2 and C2). Thus, the circuit attenuates frequencies below f = 1/(2πR1C1) = 5.9 Hz and frequencies above f = 1/(2πR2C2) = 15915 Hz. The op-amp acts like an inverting amplifier with a gain of -R2/R1=37. The voltage divider (R4 and R5) is used to give the output about a 1.6 V DC offset. This allows both the positive and negative portions of the input to be amplified and present in the output. After building this circuit, I discovered a very similar circuit is implemented by SparkFun’s electret microphone breakout board but different resistor and capacitor values are used yielding a different gain and different band-pass characteristics, a different op amp is used, and a capacitor is placed in parallel with R4 presumably to stabilize the voltage to the non-inverting input of the op-amp. This additional capacitor may be a good idea, but I actually think my circuit is superior in that the voltage divider introduces a smaller DC offset that more closely matches the midpoint of the op-amp’s dynamic range when powered by 5 volts. Just my opinion.

IR Photodiode Circuit Schematic

Posted April 4th, 2013. Filed under Basics Electronics Guides

The following schematic demonstrates a simple infrared (IR) photodiode circuit using a analog-to-digital converter (ADC) to monitor the photodiode. An analog pin on an Arduino used with the analogRead() function will do the job. Photodiodes are two-pin analog devices. This makes them relatively easy to use and makes their output graded. Some of my students are using one in a project along with an IR LED as an IR absorption meter. On the other hand, their output is relatively weak so they can’t detect extremely small or distant IR signals. Notice that the photodiode is reverse biased. Thus, when no light is present, no current flows through the diode and the 1 megaohm pull-up resistor ensures that the ADC monitoring the photodiode reads 5 V. When light, in this case IR light, strikes the photodiode, electron-hole pairs are created in the semiconductor which creates a small current causing the voltage at the ADC pin to drop. So the lower the voltage read by the ADC, the more IR light is being received.

The ATmega328 has a total of three input/output ports, port B, port C, and port D. Two of these ports, namely port B and port D, are associated with the 14 digital input/output pins on the Arduino Uno. Port C is associated with the analog input pins which can also be used as digital input/output pins if needed. The following table summarizes the mapping between the Arduino Uno digital pins and the ATmega328 port pins.

Arduino Digital Pin Port Pin
0 PD0
1 PD1
2 PD2
3 PD3
4 PD4
5 PD5
6 PD6
7 PD7
8 PB0
9 PB1
10 PB2
11 PB3
12 PB4
13 PB5


 

To turn on or off a Arduino digital pin, its mode is first set using pinMode() and then its state is set using digitalWrite(). For example, to turn on digital pin 5 for one second then turn if off for one second repeatedly, the following code could be used.


int myPin = 5;
void setup() {
   pinMode(myPin,OUTPUT);
}
void loop() {
   digitalWrite(myPin,HIGH);
   delay(1000);
   digitalWrite(myPin,LOW);
   delay(1000);
}

It is also possible to control the digital pins by accessing the port registers directly. For example, the following achieves the same result as the above code. It is explained below.


void setup() {
   DDRD = DDRD | B00100000; 
}
void loop() {
   PORTD = PORTD | B00100000;
   delay(1000);
   PORTD = PORTD & B11011111;
   delay(1000);
}

Each port has a one byte register defining the direction of the associated pins. This register is called DDRD for port D and DDRB for port B. From zero to seven, each bit in this register corresponds to the comparably numbered pin of that port. For example, the fifth bit of DDRD controls port pin D5 or Arduino digital pin 5. If this bit is set to zero, the pin is an input. If this bit is set to one, the pin is an output pin. Thus, the line of code in the setup function above sets Arduino digital pin 5 to an output pin using a bitwise or operator. Each port also has a one byte register defining the state of the associated pins. This register is called PORTD for port D and PORTB for port B. Just like the DDRx registers, each bit in these registers from zero to seven corresponds to the comparably numbered pin of that port. For example, the fifth bit of PORTD controls the state of pin D5 or Arduino digital pin 5. If this bit is set to zero, the pin is set to 0 V. If this bit is set to one, the pin is set to 5 V (on the Arduino Uno). Thus the lines of code in the loop function above first turns on Arduino digital pin 5 using a bitwise or and then turns this pin off using a bitwise and.

Controlling digital i/o pins at the register level is incredibly powerful. For example, you could turn all 8 pins of port D using a single line of code if you wanted to. However, with great power comes great responsibility. Manipulating registers can be tricky and may yield unexpected results. For example, buried in the ATmega328 data sheet is a warning that one can not change a pin from the high impedance input off state to the output on state in one step. You must first go to an intermediate state of output low or input high before going to output high.