Page 1 of 2

Light Sensor

Posted: Sun May 08, 2022 1:21 pm
by BAC-ITSUPPORT
Hi

I am using BL0011 progammer and trying to switch an output on and off depending on light conditions. The programme downloads and switches on the LED but does not switch off if I cover the sensor to simulate darkness. I have attached the file

Can you please help

Nev

Re: Light Sensor

Posted: Sun May 08, 2022 3:04 pm
by medelec35
Hello.
Can you explain what hardware you have got, e.g the grove light sensor or an LDR with a resistor or a - BL0114 - Combo board etc.
I have attached an example for you to play with.
First, you need a continuous look otherwise your code will run one from top to bottom then end.
Nothing else will happen on your hardware
The groove component on Flwocde is just an analogue to digital converter (ADC)
As you are using byte , the value will be 0 to 255
The brighter the light the higher the value.
For example dim light, byte could = 50
Bright light, byte could = 220.
What your flowchart needs to do is have a forever loop, Read the light intensity, and then act on the result of the light intensity.
You will need to check the sensor and LED properties are connected to the correct pins of your hardware.
When you run the simulation, when the slider is below 0.5 the LED will light & when the slider is above 0.50 the LED will go out.
What you recommend you do is to go to here, and select Introduction to Microcontroller Programming
Download and work your way through the course.

Re: Light Sensor

Posted: Mon May 09, 2022 10:34 am
by BAC-ITSUPPORT
Hi

Thanks for your help, Whilst the simulation operates ok I have the same issue in that the output D0 is always on regardless of light condition. I have the following set up. EBLOCKS 2 Grove board BL0129-1 connected to a grove light sensor. I have set the channel input to An0 as the grove board is connected to port 1. Can you please help I have attached a file and a photo of the set up

Re: Light Sensor

Posted: Mon May 09, 2022 12:28 pm
by medelec35
What I would recommend is testing that the sensor is working as expected.
If you load the attached flowchart onto your hardware you expose and shade the light going to the sensor and observe the value via the LEDs.
Each LED from 0 to 7 increases by the power of 2 ,i.e doubles in value.
Therefore LED 0 = 1, LED1 = 2, LED2 = 4, LED3 = 8, LED4 = 16 ........LED7 = 128
First partly shade the sensor so it's at the level at which you want the threshold of the LED to light
You will need to look at what LEDs are on and then add together all the values.
E.g LED1, LED3, LED5 & LED6 are the only ones on the the value =

Code: Select all

 2 + 8 + 32 + 64  = 106
If you want the LED to go on when the sensor is partly shaded, set the value for

Code: Select all

LightIntensity > 106
If the LEDs don't change with brightness then there could be a hardware issue.
You can measure the voltage across the signal of the sensor and ground.
The signal voltage should be Supply voltage/256 * value derived from lit LEDs
Using 106 as the example and the supply voltage is 5V
Then the voltage across the signal pin (A0 id the micro) should be 5 / 256 * 106 = 2.07V
If I was you and the LEDs 0 to 7 do change value with brightness, it will be worth noting the full light and fall dark values.

Re: Light Sensor

Posted: Mon May 09, 2022 2:07 pm
by BAC-ITSUPPORT
Hi

Thanks for your recent post I have resolved the issue I had it is now working as I need it.

Thanks for your help

:D

Re: Light Sensor

Posted: Mon May 09, 2022 2:53 pm
by medelec35
That's great.
Thank you for letting us know.

Re: Light Sensor

Posted: Tue May 10, 2022 7:03 am
by habba34
how do u start the code

Re: Light Sensor

Posted: Tue May 10, 2022 8:13 am
by medelec35
Can you explain what you mean in more detail?
For beginners, there is a great free course to follow called Introduction to Microcontroller Programming.
If you don't have the hardware, just use the simulation capabilities.
Alternatively, there is a get starting guide.
It's still great, but does not cover as much or does not have teaching exercises as the above course.

Re: Light Sensor

Posted: Tue May 10, 2022 3:27 pm
by habba34
so ive got the code you have given is it supposed to be noticable what is happening on debug or is it just on the hardware

Re: Light Sensor

Posted: Tue May 10, 2022 3:28 pm
by habba34
and is that the final code or do bits need adding