The Trinket 5V, photoresistors, and you

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ebenollie
 
Posts: 1
Joined: Sun Sep 05, 2021 4:47 pm

The Trinket 5V, photoresistors, and you

Post by ebenollie »

SO I was originally posting this as a question but I rubber ducked my way to success over the course of several drafts over several weeks of off and on fiddling and downtime due to dread. Posting for anyone having trouble with the trinket 5V and photoresistors in the future:

My project for undercabinet lights includes a photoresistor among other components for night lights and automatic shutoff.

I tested all components individually on an Uno, and got the essentials working on the Trinket, but when trying out the photoresistor it's a flop. I had not gotten a photoresistor to work on the trinket at all and was wondering if this board was going to work. The Uno fell in the line of duty so testing it out on the old girl was not an option. I think I fried the USB Serial chip.

Code and circuit as found on https://www.instructables.com/How-to-us ... rduino-Tu/

Code: Select all

/* Use a photoresistor (or photocell) to turn on an LED in the dark
   More info and circuit schematic: http://www.ardumotive.com/how-to-use-a-photoresistor-en.html
   Dev: Michalis Vasilakis // Date: 8/6/2015 // www.ardumotive.com */
//Constants
const int pResistor = 2; // Photoresistor at Arduino analog pin A1
const int ledPin = 1;       // Led pin at Arduino pin 1

//Variables
int value;          // Store value from photoresistor (0-1023)

void setup(){
 pinMode(ledPin, OUTPUT);  // Set lepPin - 1 pin as an output
 pinMode(pResistor, INPUT);// Set pResistor - A1 pin as an input (optional)
}

void loop(){
  value = analogRead(pResistor);
  
  //You can change value "25"
  if (value > 25){
    digitalWrite(ledPin, LOW);  //Turn led off
  }
  else{
    digitalWrite(ledPin, HIGH); //Turn led on
  }

  delay(500); //Small delay
}
What I had tried:
Analogread on pin 3. Searches showed all the weirdness with pin 3's pull up resistors so I moved on.
Analogread on pin 2. LED would not turn on.
Analogread on pin 4. LED would not turn off.
These last two factors had lead me to believe it was a hardware issue.

Analogread value threshold tested from 15 to 225 did not change the last two results. None of the tutorials for photoresistors mention using the map() function like with an analog knob so I didn't bother.

I'm paranoid about due diligence when asking for help so I went back and tried pin 3 to be thorough. My analog knob sketch had worked perfectly on pin 3 before I learned about how it wouldn't so for grins I set the threshold value higher, at 500. Nothing. Set it back to pin 4 on 500. Color me salty when it worked. After some more trial and error I worked out 235 is sensitive enough on pin 4 to activate within a foot of my lamp.

Now I don't have to buy a different board. Yay me.
If I had asked outright would some kind soul here have sent me on the straight and narrow and saved three weeks? Let's not worry about that. Instead, let's hope my toiling helps at least one person in the future.

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”