Quiz 2:
1. An analog sensor physically outputs variable voltage and allows a microcontroller to read a range of values. An example of an analog sensor is a photocell light sensor. A digital sensor physically outputs on/off or high/low, can only output a certain voltage or no voltage. Digital sensors allow a microcontroller to detect binary state changes. And example of a digital sensor is a push button switch.
2.
3. A pulldown resistor "pulls down" remaining voltage, removing any chance for electrical interference when sensing using a microcontroller. It ensures that there is no floating voltage in any given component that could skew readings and cause undesirable results. It enforces a known state for sensor signals.
4.
void setup() {
// set the inputs and outputs
pinMode(2, INPUT); // set pushbutton pin to be an input
pinMode(3, OUTPUT); //set led 1 to be an output
}
void loop() {
// read the pushbutton input:
if (digitalRead(2) == HIGH) {
//if push button is closed: LED is ON
digitalWrite(3, HIGH);
} else {
// if push button is open: LED is OFF
digitalWrite(3, LOW);
}
}
5.
6.
byte flag = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// set inputs and outpus
pinMode (2, INPUT);
}
void loop() {
// when button is pushed, print PUSHED
if (digitalRead(2) == HIGH && flag == 0) {
Serial.println ("PUSHED");
flag = 1;
}
//When button is released, print RELEASED
if (digitalRead(2) == LOW && flag == 1) {
Serial.println ("RELEASED");
flag = 0;
}
}
Lab 1: Tone
This lab showed us how to hook up the speaker to modulate the volume and/or frequency using an analog sensor.
Tone output using pressure sensor
Here's the code:
void setup() {
}
void loop() {
int sensorReading = analogRead(A0); //read analog input
float frequency = map(sensorReading, 0, 1000, 500, 1500);
tone(8, frequency, 10);
}
Tone output with frequency modulation using a potentiometer
Here's the Code:
void setup() {
Serial.begin(9600);
}
void loop() {
int sensor = analogRead(A0);
Serial.println(sensor);
int frequency = map(sensor, 0, 1023, 1000, 5000);
tone(9, frequency);
}
Lastly, here's me playing with a servo motor, whose angle is mapped to the pressure applied to the sensor.
Here's the code:
#include <Servo.h>
Servo servoMotor;
void setup() {
Serial.begin(9600); // initialize serial communications
servoMotor.attach(3);
}
void loop()
{
int analogValue = analogRead(A0); // read the analog input
Serial.println(analogValue); // print it
int servoAngle = map(analogValue, 50,1023,0,179);
if(millis() % 20<2) {
servoMotor.write(servoAngle);
}
}
Questions/Issues:
The labs went pretty well, but I do have a couple of questions after playing with the pressure sensor.
I couldn't find the "pitches.h" file to include for the melody lab on the website, I'm not sure if there's another location of the file so I wasn't able to complete that part of the lab - but I read through and I understand the gist of it.
I noticed that the pressure sensor has some noisy levels when nothing is touching it. I'm guessing it's just really sensitive and maybe registering air pressure, but the readings range from 0-15 with no human pressure applied. I tried using the map function to filter out those lower values, but I'm not sure if it worked.
Follow-up to the last question. I noticed when using the pressure sensor on my servo motor that the servo motor was making a buzzing noise when the sensor wasn't being touched. I again tried to filter out the lower values using the map function but it didn't work and buzzing persisted off and on. I wonder if there's something wrong with my circuit?
Comments