Lab #2: Analog Inputs and Outputs
Lab 2: Analog Inputs and Outputs
This second lab had us using analog inputs and outputs, such as sound, and we then were able to start developing more complex medical devices.
Generating Signals Inside the Microcontroller
Ramp Output
![]() |
| Using the Serial Monitor proved to not be a good visual when looking at our ramp output. While you can see our function worked, it is hard to visualize. |
![]() |
| Above is a screenshot of the Serial Plotter to better visualize the ramp output function. |
Sine Wave
![]() |
| The sine wave generated on the Serial Plotter. |
Make Some Noise - Analog Output
![]() |
| Fritzing of the circuit with the Piezo buzzer with 100k Ohm resistor |
![]() |
| Our code for the Piezo buzzer simply to make a sound. We initially had trouble getting it to beep because we did not first realize that only certain pins could be used with the Piezo. |
![]() |
| The code for our Piezo to create two different sounds. We wanted something easily differentiable but also quite simple if its intended use is for a medical device. |
Control LED Brightness - Analog Output
Analog Input - Light Sensor
Analog Input - RCTime
![]() |
| Fritzing of the circuit we used for the Lie Detector. We replaced the LED light from the RCTime portion of the lab with two wires, which are held by whoever is taking the test. |
Galvanic Skin Resistance (Lie Detector)
![]() |
| Fritzing of the circuit we used for the Lie Detector. We replaced the LED light from the RCTime portion of the lab with two wires, which are held by whoever is taking the test. |

First Major Project: Button Tap
![]() |
| The code for our button tap and we printed how many milliseconds it took for the user to do 10 button taps. |
![]() |
| Serial Monitor of Button Tap in Action |
![]() |
| Fritzing of our push button circuit with 220 Ohm resister |
First Major Project: Finger Tap Sensor
![]() |
| Fritzing of our circuit for the Finger Tap sensor. As you can tell, it is the same circuit we used for the Galvanic Skin Resistance / Lie Detector portion of this lab. |
int sensPin = 2; // 220 resistor connected to this pin
long result = 0;
bool lookingForCount = true; // used a bool because of the "true"/"false"
// so we wouldn't have to code for different instances
int taps = 0;
long startTime;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
Serial.println("Testing RCTime");
}
void loop() // run over and over again
{
long val = 100000 - RCtime(sensPin);
if (val > 98000.0 && lookingForCount) { // when testing finger tap sensor for sensPin value
// value was typically under 2000; so this if loop indicates when the sensor is being tapped
if (taps == 0) {
startTime = millis();
lookingForCount = false;
taps += 1;
} else if (taps == 10) {
taps = 0;
Serial.print("Time Elapsed in milliseconds for 10 taps:");
Serial.println(millis() - startTime);
lookingForCount = true;
} else {
lookingForCount = false;
taps += 1;
}
} else if (val < 98000 && !lookingForCount) {
lookingForCount = true;
}
delay(10);
}
long RCtime(int sensPin){
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(1); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin)){ // wait for pin to go low
result++;
// if (result == 1000) break; // don't wait forever for pin to go low
// if result reaches 100,000 stop waiting.
// this means that if RCTime returns 100,000, it really
// means that RCTime is >= 100,000.
// you can change this value of 100,000 to something else.
// there is nothing magic about the number, 100,000.
}
return result; // report results
}


















Comments
Post a Comment