There is a light sensor on your picoprobe development board. This question asks you to use this sensor to measure the ambient light and to control the three LEDs on the picoprobe board to make a night light. Configure your program to perform the following steps, after printing the message from Question 1. The steps should be repeated every 1 second. raw ADC result in the format shown below: Light Sensor: Raw: _____ (insert the actual value) (15 marks) (b) Convert the raw value to a voltage and print out the voltage that you calculated in the format displayed below. At least one decimal place is required. Voltage: _____ V (insert the actual value) (10 marks) (c) Use this voltage to control the three-colour LED to show a white colour, when the light sensor is covered by a piece of paper (e.g. when the room is getting dark). The white-coloured light will go off, as soon as the ambient light is high again, i.e. when the light sensor is exposed to normal room light. This is reminiscent of an automatic night light.
时间: 2024-02-14 17:07:38 浏览: 23
Sure, here's how you can use the light sensor on the picoprobe development board to create a night light:
(a)
1. Initialize the ADC to read from the light sensor.
2. Read the raw ADC value from the light sensor.
3. Print out the raw ADC value in the format "Light Sensor: Raw: [value]".
4. Convert the raw ADC value to a voltage using the formula: voltage = (raw_value * 3.3) / 4095, where 3.3 is the maximum voltage and 4095 is the maximum raw ADC value.
5. Print out the voltage value in the format "Voltage: [value] V".
6. Use the voltage value to control the three-colour LED.
(b)
Here's some sample code to perform the above steps:
```c
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/gpio.h"
#define LED_RED 25
#define LED_GREEN 26
#define LED_BLUE 27
#define LIGHT_SENSOR 2
int main() {
stdio_init_all();
adc_init();
adc_gpio_init(LIGHT_SENSOR);
adc_select_input(LIGHT_SENSOR);
gpio_init(LED_RED);
gpio_set_dir(LED_RED, GPIO_OUT);
gpio_init(LED_GREEN);
gpio_set_dir(LED_GREEN, GPIO_OUT);
gpio_init(LED_BLUE);
gpio_set_dir(LED_BLUE, GPIO_OUT);
while (1) {
uint16_t raw_value = adc_read();
printf("Light Sensor: Raw: %d\n", raw_value);
float voltage = (raw_value * 3.3) / 4095;
printf("Voltage: %.1f V\n", voltage);
if (voltage < 1.0) { // dim the LEDs when the room is dark
gpio_put(LED_RED, 255);
gpio_put(LED_GREEN, 255);
gpio_put(LED_BLUE, 255);
} else { // turn off the LEDs when there is enough ambient light
gpio_put(LED_RED, 0);
gpio_put(LED_GREEN, 0);
gpio_put(LED_BLUE, 0);
}
sleep_ms(1000);
}
return 0;
}
```
(c)
In the above code, we are dimming all three LEDs to create white light when the room is dark and turning them off when there is enough ambient light. The threshold voltage for detecting darkness is set at 1.0 V in this example, but you can adjust it to suit your needs.