---
description: Working circuit with led and sensor
---

# Assignment 2 - Working circuit

## 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.

![Back side crystal sensor, Dusanka, Loes, 2020](https://2472482582-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzaCPWnWmmX17D0w0O3%2F-M2hAFYe25TXlPJpBS2w%2F-M2hHxQicfB8h93IBaHL%2FWhatsApp%20Image%202020-03-18%20at%2011.56.30.jpeg?alt=media\&token=42ee922a-ab02-4e27-a71c-5be87d8d3c75)

![Front side crystal sensor, Dusanka, Loes, 2020](https://2472482582-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzaCPWnWmmX17D0w0O3%2F-M2hAFYe25TXlPJpBS2w%2F-M2hI4Faj9xlpFk7GFNY%2FWhatsApp%20Image%202020-03-18%20at%2011.56.49.jpeg?alt=media\&token=ec58dbb2-62bf-49ba-9de2-294a261df3a3)

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](https://dlo.mijnhva.nl/content/enforced/32318-FDMCI-6100C08A19-DMCI-CMD-1920/02_LDR_sensor_with_LED_simple_NODEMCU.txt?_\&d2lSessionVal=ssElxMxAH4d3yRJRQz2Ap22q0).

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.

![Crystal sensor with 2 leds, dusanka, 2020](https://2472482582-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzaCPWnWmmX17D0w0O3%2F-M2hAFYe25TXlPJpBS2w%2F-M2hK8626qWFPCnNHSMV%2FWhatsApp%20Video%202020-03-18%20at%2012.08.04.mp4?alt=media\&token=c58ed217-34e2-4d61-8342-fbefb784862f)

```
/*
  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.

![End result Paper circuit with sensor & 2 leds, dusanka, 2020 ](https://2472482582-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LzaCPWnWmmX17D0w0O3%2F-M2hAFYe25TXlPJpBS2w%2F-M2hLpyYU4GnvBh7y1F2%2Fezgif.com-video-to-gif%20\(3\).gif?alt=media\&token=058b7442-7f09-421e-b06f-b336be38db21)

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.
