#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#define IR_SENSOR_PIN 26 // GPIO 26 is ADC0
int main() {
stdio_init_all();
// Initialize ADC
adc_init();
adc_gpio_init(IR_SENSOR_PIN); // Enable ADC function on GPIO 26
adc_select_input(0); // Select ADC0 (GPIO 26)
printf("Analog IR Sensor Test\n");
while (true) {
// Read the analog value from the IR sensor
uint16_t raw_value = adc_read(); // ADC value (0-4095)
float voltage = (raw_value * 3.3) / 4095.0; // Convert to voltage (0-3.3V)
// Print the raw value and voltage
printf("IR Sensor Value: %u, Voltage: %.2fV\n", raw_value, voltage);
sleep_ms(500); // Delay for readability
}
}
/*this is the digital form of it
#include <stdio.h>
#include "pico/stdlib.h"
#define IR_SENSOR_PIN 26 // Pin connected to the IR sensor's OUT pin
int main() {
// Initialize stdio for debug output
stdio_init_all();
// Initialize the IR sensor pin as input
gpio_init(IR_SENSOR_PIN);
gpio_set_dir(IR_SENSOR_PIN, GPIO_IN);
printf("IR Sensor Test Initialized\n");
while (true) {
// Read the state of the IR sensor
bool isObstacle = gpio_get(IR_SENSOR_PIN);
if (isObstacle) {
printf("No Obstacle Detected\n");
} else {
printf("Obstacle Detected!\n");
}
sleep_ms(500); // Check every 500ms
}
}
*/