Assignment 2 - Working circuit
Working circuit with led and sensor
Crystal analog switch
I used Loes' examples as a reference for the crystal sensor. I soldered everything together like she had done before with hers. I tried using the thick copper steel but unfortunately I had nothing to break and bend it with so I had to use the thin copper wire which was hard to solder on the copper tape.


Then I connected the crystal sensor like I did before with the paper LEDs and button to the breadboard to see the outputs of the sensor. I used Loes' code.
I got really weird outputs in the beginning in the serial monitor, but this was because I didnt match the baud with the serial.begin and upload speed. After Loes told me this and I changed it the sensor was working!
Then I connected 2 LEDs with the sensor to make it a bit different than Loes' example and I changed the code a bit with an if/else statement.
/*
created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe
modified 13 March 2020
By Loes Bogers
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPinRood = D1; // select the pin for the LED that has PWM (~) e.g pin D1 on Node MCU
int sensorValue = 0; // variable to store the value coming from the sensor
int mappedValue = 0; //store the mapped values
int ledPinGroen = D2;
void setup() {
// initialize serial communication with computer:
Serial.begin(115200); //make sure it matches with the number in the serial port
// declare the ledPin as an OUTPUT:
pinMode(ledPinRood, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(ledPinGroen, OUTPUT);
}
void loop() {
delay(1000);
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
//// FIND MIN & MAX RANGE FIRST, THEN COMMENT OUT
// print values to serial to find lowest and highest values (min and max) in serial monitor
Serial.println(sensorValue); // met 10K voltage divider: bijv. range 100-850 zonder een fietslampje erbij (beter voor de video)
//PUT THE VALUES YOU FOUND (YOUR_MIN, YOUR_MAX) INTO THE LINE BELOW (line 36)
//put your min value and max value (as seen in the monitor) and map to a range 0- 255 for output
mappedValue = map(sensorValue, 242, 1024, 0, 255);
// print values for debugging
Serial.print("Old Value (no mapping) = ");
Serial.print(sensorValue);
Serial.print("\t"); // add a tab between the numbers
Serial.print("New value (mapped) = ");
Serial.println(mappedValue);
if (sensorValue > 300) {
digitalWrite (ledPinRood, HIGH);
digitalWrite (ledPinGroen, LOW);
}
else {
digitalWrite (ledPinRood, LOW);
digitalWrite (ledPinGroen, HIGH);
}
}
If the sensor will catch light, the red led will go on and the green led is off and vice versa.
Then I tried to make the sensor in another kind of shape. Unfortunately because I understood the assignment a bit wrong and went so many ways because I didnt understood the circuit at first, I didn't had much time left for the actual creative part so I made a simple box. I would've loved to put more effort and time in this.

Box open = Red ON, green OFF Box closed = Green ON, red OFF.
Reflection
To be honest I'm not too proud of what I've made. It all took so much time because I couldn't understand how the circuits were working. I discovered that I was having trouble with understanding the assignment first, and then begin at the very first step. Instead of diving into the crystal sensor and being in a cycle of not understanding it. If I had started from the beginning I would've had more time left to make something nice. But I am happy that it is working at the end, it took a lot of time and patience.
Last updated