Using Resistor as a Switch



You might have seen many people using photoresistor to control something, but do you know that you can use normal resisters as switches too.  Here is one way you can make secret key with normal resistor. Just set the value you want and the Arduino will turn on/off something at the set value.  In the below sketch I've set the value between 507 to 513. That means only resistor with that value can be work as a switch and no other resistor.
The sketch
int resistorPin = 0;     // connect the resistor to a0
int resistorReading;     //
int led = 4;          // connect LED to pin 4
    void setup(void) {

      pinMode(led, OUTPUT);
      Serial.begin(9600);
    }
  
    void loop(void) {
      resistorReading = analogRead(resistorPin);    
      Serial.print("Analog reading = ");
      Serial.println(resistorReading);     // the raw analog reading
   
if (resistorReading >=507 && resistorReading <=513){
    digitalWrite(led, HIGH);
  }
  else {
    digitalWrite(led, LOW);
  }
      delay(200);
    }

No comments:

Post a Comment



Newly posted:
Reason why rich getting richer and poor getting poorer