Lab 3: Analog Sensor Box

 

In part one of the lab, we created a circuit with variable input. In my case, I decided to use a pressure sensor and a potentiometer to change the brightness on LEDs.

In order to create this circuit, I first drew out a plan.

IMG-1539

Next, I connected the potentiometer and used the serial monitor to confirm that it was working. It connected to power, ground, and pin A0.

Then, I added the pressure sensor and used the serial monitor to confirm that both sensors were working. It connected to pin A1 and power, with a 10K drop down resistor.

Once I confirmed that both of the sensors were giving me input, I added the LEDs in series (each with a 220ohm resistor).

img-1496.jpgIMG-1497IMG-1498

The code for illuminating the lights using input from the sensors is below.


int pot = 0; //potentiometer variable
//adding for a photoresistor
int pressure = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize serial
}
void loop() {
// put your main code here, to run repeatedly:
pot = analogRead(A0);
pressure = analogRead(A1);
Serial.println(pot);
Serial.print(" pressure ");
Serial.println(pressure);
int lightVals = map(pot, 0, 1023, 0, 255);
int light2Vals = map(pressure, 0, 1007, 0, 255);
analogWrite(10, lightVals);
analogWrite(9, light2Vals);
}

At first, the light connected to the pressure sensor wasn’t working, so I thought I did something wrong. I tried changing the LED though, which worked, so I guess I just had a dead LED. Yay for easy fixes! Here is a video of the sensors dimming the lights.

Part two of the lab involved using two of the lab was to use two photo-resistors to determine the tone a speaker is emitting. This is interesting because normally when you use variable resistors, you use a voltage divider circuit. However, instead of using a fixed resistor as the voltage divider, we are using a second variable resistor. I understand how this works after learning a little more about potentiometers, because that is essentially what they are. So, potentiometers work because they are made of a wiper (strip of conductive metal and a ring of non-conductive or insulation material (usually some sort of carbon, I think). The closer the wiper is to power, the less resistance between power and the pin (which is connected to signal, which is connected to the wiper) and then more between the pin and ground. The closer the wiper is to ground, the more resistance between it and the pin. Thus, a variable resistor.

Most variable resistors don’t have this kind of voltage divider circuit built in though, so you have to make it. This usually involves adding a fixed resistor that goes to ground, but in this case, is a second photoresistor. Pretty neat!

I got the speaker to make sounds pretty easily, though determining the range of the sensors was a bit of a task. Here’s some pictures and a video of the terrible noises it makes!

IMG-1541IMG-1542IMG-1543IMG-1544

And here is the code that I used to make it make those terrible noises!


int photo = 0;
//adding for a photoresistor
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize serial
}
void loop() {
// put your main code here, to run repeatedly:
photo = analogRead(A0);
// Serial.println(photo);
int toneVal = map(photo, 130, 900, 100, 1000);
tone(8, toneVal, 10);
}

view raw

labthreeP2.ino

hosted with ❤ by GitHub

Part three of the lab was to combine part one and two and then shove them in a box. Easy-peasy. NOT. I needed two inputs and two outputs, which doesn’t sound like an excessive number of things to put in a box. OR SO I THOUGHT! Then, things went awry and I ended up with my breadboard and arduino shoved in the box, which was not the original plan. I based the dimensions of my box on just having the arduino fit in it, with all the components on break out boards, but quickly realized a few things:

  1. I didn’t have enough power and ground pins on the arduino for all the components.
  2. I didn’t have the time to perfect the way that components would need to be laid out on the breakout boards.
  3. I didn’t have time to solder breakout boards, and I really didn’t have time to solder them wrong, unsolder them, and redo them.

So, lame, yes. But still real problems. Hence, the breadboard is shoved into the box. On the positive side, it is a super tight fit, so it should meet Arielle’s expectation of “the box doesn’t make noise if you shake it”. Here is the original plan for the box and the electrical schematic:

img-1556.jpgimg-1555.jpgIMG-1556img-1557.jpg

And here is the box in progress:

 

 

 

 

 

Here is the code that allows it make irritating noises and bright lights:


int photo = 0; //photoresistor variable
int pot = 0; //potentiometer variable
void setup() {
// put your setup code here, to run once:
pinMode(5, OUTPUT);
Serial.begin(9600); // initialize serial
}
void loop() {
// put your main code here, to run repeatedly:
photo = analogRead(A0);
pot = analogRead(A2);
Serial.print("pot: ");
Serial.print(pot);
Serial.print(" photo: ");
Serial.println(photo);
//Making the speaker controlled by the potentiometer
int speakerVal = map(pot, 0, 1023, 100, 1000);
tone(8, speakerVal, 10);
//this works.
//Now make photo resistor talk to lights. Make the light turn on if a certain level of dark is reached
//and turn off if it is bright out.
int lightVal = 0;
if(photo<700){
lightVal = 255;
} else{
lightVal = 0;
}
analogWrite(5, lightVal);
}

view raw

labThreep3.ino

hosted with ❤ by GitHub

I’ve decided to call it the box of annoyance. I can market it to younger siblings, very useful for driving older siblings insane. Here is the finished annoyance box product (order yours today!):

Here’s a few images of the completed box since you can’t see what is is made of at all in the video (P.S. is is mostly made out of hot glue and tears):

Leave a comment

Design a site like this with WordPress.com
Get started