move servomotors with photoresistor

Hello guys, I have a very simple circuit with a led and a photoresistor and a servomotor, what I want to do is, as the photoresistor is receiving light, the servomotor will be moving at the same time. Everything happening at the same time, in this case, I have measured the values that the photoresistor receives when it is receiving light and the values go from up to down, so if you can help me I would appreciate it, thank you

How does this relate to 'website and forum'? I've asked a moderator to move it.

I have measured the values that the photoresistor receives when it is receiving light and the values go from up to down

Please explain this better.

If you used code to measure, please show the code that you used (and use code tags)

Type [code]
Paste your code after that
Type [/code] after the pasted code

Draw a little diagram, take a photo or scan it and post it here. If it was not connected to the Arduino when you were doing the measurement (you e.g. used a multimeter), that's sufficient. Indicate how you think it should be connected to the Arduino. If it was connected to the Arduino when you were doing the measurement, indicate how it was connected.

Do read this, it'll help you a great deal.

johnbroach: Hello guys, I have a very simple circuit with a led and a photoresistor and a servomotor, what I want to do is, as the photoresistor is receiving light, the servomotor will be moving at the same time.

Everything happening at the same time, in this case, have measured the values that the photoresistor receives when it is receiving light and the values go from up to down, so if you can help me I would appreciate it, thank you

What does the LED do? Although everything may appear to move at the same time, there is a process occurring. How is the LDR's signal being used? Does it operate the servo or does the servo just sweep from 0 to 180 and back continuously? Thanks.. Tom.. :)

#include <Servo.h>
Servo servo1;
int photores=A0;



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  servo1.attach(9);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
photores=analogRead(A0);
Serial.println(photores);



}

I tried many ways of doing it but I do not get the idea, that is why i have the servo declaration

here’s the image of the circuit
Imgur

What does the LED do?
Although everything may appear to move at the same time, there is a process occurring.
How is the LDR’s signal being used?
Does it operate the servo or does the servo just sweep from 0 to 180 and back continuously?

Yes, the LDR’s signal operates the servo

Good start. Now you should look at what values you get from your LDR, and decide at which value you want to move the servo.

What I think you’re trying to do is move the servo to either 0 or 180 degrees, depending on the light level. Above a certain level one way, below that the other way. Right?

void loop() {
  unsigned int photores=analogRead(A0);
  if (photores > 500) moveServoOneWay();
  if (photores < 300) moveServoTheOtherWay();
}

Note the difference for the two movements: that’s the hysteresis you need to not start moving the servo back and forth when the light level is about at the trigger point.

Another solution for this (especially if you have short flashes of light or short shades passing by) would be to add a timeout: light has to be below/above a certain level for a certain amount of time before the servo reacts.

What I think you're trying to do is move the servo to either 0 or 180 degrees, depending on the light level. Above a certain level one way, below that the other way. Right?

Well, it is not possible that the servomotor will move at the same time as the photoresistor is receiving light?, I mean, as the led is approaching the photoresistor, the servomotor moves simultaneously. It is because I am creating a robotic hand, and I will place five different LEDs with their respective photoresistors on a glove in each finger, and I want to copy the movement of my hand to represent it with a robotic hand with servomotors to copy my movements

If you had just described your project more in the first place, we'd be getting there faster. Is there any more information you're holding back?

In this case: figure out the values you get for dark and light, them map those to 0-180 (look at the map() function), and feed that result to your servo.

If you had just described your project more in the first place, we'd be getting there faster. Is there any more information you're holding back? No that is all

When it is dark the value is 900 more or less, it starts to detect light at 800 and my last value is 260 when the less reaches the maximum or closer level

When it is dark the value is 900 more or less, it starts to detect light at 800 and my last value is 260 when the less reaches the maximum or closer level

Care to try that sentence in English? I recognise the words but can’t make them convey any meaning.

Care to try that sentence in English? I recognise the words but can't make them convey any meaning.

What I meant was, that when there is no light reaching the photoresistor, the value that it has is 900 approximately, and when i bend my finger a little bit, the value that I get from the photoresistor is 800 and the last value is 260 when the led is so close to the photoresistor(all in the serial monitor of Arduino)

johnbroach: What I meant was, that when there is no light reaching the photoresistor, the value that it has is 900 approximately, and when i bend my finger a little bit, the value that I get from the photoresistor is 800 and the last value is 260 when the led is so close to the photoresistor(all in the serial monitor of Arduino)

Hi, Have you got this happening? Have you got those sort of values as you move from far to closest approach now, or do you wish that those values happen? Have you got a glove setup with one LDR and LED to do a proof of concept. How are you going to make sure the LED on one finger does not effect the LDR on another as you move your fingers. To simply "map" the LDR values to 0 to 180 or what ever range of servo movement you need, look at the map function. https://www.arduino.cc/reference/en/language/functions/math/map/ Also you will need a to look at how much power you need for the servos. Tom.. :)

Yes, I got that values as the led approached the photoresistor, I’ve placed just a led and its respective ldr on one finger and I have got the same readings.

How are you going to make sure the LED on one finger does not affect the LDR on another as you move your fingers.

Well this something I hadn’t thought about

#include <Servo.h>
Servo servo1;
int photores=A0;
int angulo=0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  servo1.attach(9);
  
}

void loop() {
  // put your main code here, to run repeatedly:
 
{
  if(photores==300)angulo+=45;
  else if(photores<300)angulo=0;
  angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}
{
if(photores==400)angulo+=45;
  angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}
{
if(photores==500)angulo+=45;
  angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}
{
if(photores==600)angulo+=45;
  angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}
 {
if(photores<=700)angulo+=45;
  angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
} 
{
if(angulo=180&photores==600)angulo-=45;

photores=analogRead(A0);
Serial.println(photores);
 angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}
{
if(angulo=135&photores==500)angulo-=45;

photores=analogRead(A0);
Serial.println(photores);
 angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}
{
if(angulo=90&photores==400)angulo-=45;

photores=analogRead(A0);
Serial.println(photores);
 angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}
{
if(angulo=45&photores==300)angulo-=45;

photores=analogRead(A0);
Serial.println(photores);
 angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}
{
if(angulo<45&photores<300)angulo-=45;

photores=analogRead(A0);
Serial.println(photores);
 angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}
}

I’ve made something like this but I’m not sure,one part is when the hand i being bended and the other is when the ahnd is being realesed

Also you will need a to look at how much power you need for the servos.

Thanks Tom

Hi,

{
if(photores==500)angulo+=45;
  angulo=(angulo,0,180);
  servo1.write(0);
  Serial.println(angulo);
  delay(10);
}

You need to check how you structure an if… statement.
Also in your code you are writing a value of 0 to the servo not angulo that you are printing on the monitor.

https://www.arduino.cc/reference/en/language/structure/control-structure/if/

Also trying to act on an exact value will cause the servo to jitter, because in the above example, you will not be able to hold a value of exaclty 600.

if(photores>450 && photores < 550)

Will give you a relatively stable range.

Did you look at the map function linked for you?
You will not need a string of if… statements.

Tom… :slight_smile:

Hi,
You also need to analogRead(photores)

https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/

Tom… :slight_smile:

#include <Servo.h>
Servo servo1;
int photores=A0;
int angulo=0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  servo1.attach(9);
  
}

void loop() {
  // put your main code here, to run repeatedly:
 

  photores=analogRead(A0);
  angulo=analogRead(A1);
  angulo=constrain(angulo,0,180);
  analogWrite(A1,angulo);
  {
  if(photores>250 && photores < 550)
  angulo+=36;
  servo1.write(angulo);
  
  
}
{
if(photores>350 && photores <450)
 angulo+=36;
  servo1.write(angulo);
 
}
{
if(photores>450 && photores <550 )
  angulo+=36;
  servo1.write(angulo);
  
}
{
if(photores>550 && photores<650 )
  angulo+=36;
  servo1.write(angulo);
  
}
 {
if(photores>650 && photores<750)
  angulo+=36;
  servo1.write(angulo);
  
} 
{
if(angulo=180 && photores>550 && photores<650)
 angulo-=36;
  servo1.write(angulo);
  
}
{
if(angulo=135 && photores>450 && photores<550)
angulo-=36;
  servo1.write(angulo);

}
{
if(angulo=90 & &photores>350 && photores>450)
angulo-=36;
  servo1.write(angulo);
 
}
{
if(angulo=45 && photores>250 && photores >350)
angulo-=36;
  servo1.write(angulo);
  
}
{
if(angulo<45 )
angulo-=36;
  servo1.write(angulo);
  delay(10);
}
}

So is it something like that?

Hi, mate.
You are gettig there.
I like the inclusion of the constraint… function as the map is just a simple linear formula for a striaght line and calculates out side the sample points as well as inside.

I have had a edit of your code, to show how to use the map function and transfer it to servo.
If you open the IDE monitor and select 9600 baud, the displaytomonitor function will display your LDR and Servo Angle values.
I have slowed the loop to loop every 500ms, but you can play with that to slow or speed it up.
You may find the servo will jitter, if so then there will need to be some more code written.

I hope I have commented it enough for you to understand and play with.
The 260 , 900 and 0 , 180 values in map may have to be adjusted to suit your servo range.

#include <Servo.h>
Servo servo1;
int photoresInpPin = A0;
int photoresVal = 0;
int angulo = 0;
int servo1Pin = 9;


void setup()
{
  Serial.begin(9600);
  servo1.attach(servo1Pin);
}

void loop()
{
  photoresVal = analogRead( photoresInpPin);     // read the LDR
  angulo = map(photoresVal, 260, 900, 0, 180);   // maps 260 to 0deg all the way to 900 to 180deg
  angulo = constrain(angulo, 0, 180);            // constrains the value of angulo
  servo1.write(angulo);                         // commands the servo                                   
  dipsplaytomonitor();
  delay(500);                                   // slows the read cycle
}
//===============================

void dipsplaytomonitor()      // function to display values
{
  Serial.print("LDR Value \t");    //   the \t are the equivalent of tab instructions
  Serial.print(photoresVal);
  Serial.print("\t Servo Angle \t");
  Serial.print(angulo);
  Serial.println(" degrees");
}

Tom… :slight_smile:

I hope I have commented it enough for you to understand and play with. The 260 , 900 and 0 , 180 values in map may have to be adjusted to suit your servo range.

Yes, Tom, thanks a lot, I got it, I added the void to display the values, and I guess that'll be all right? Because I do not see any problem with the code, anyway if you have any other relevant comment I would appreciate it. I'll just need to implement it with the glove. I just need to check the program with the systems working because now the next challenge would be to think of something for the light of a led does not interfere with the others

Hi, You could try a small piece of tubing around the LDR and LED so their field of view is more confined.

Tom... :)