bonjour j’ai un capteur de mesure d’UV et de lumière ambiante que je n’arrive pas à lancer les mesures correctement dans la raspberry pi 3b avec l’I2C or que je n’oublie pas de configurer les registres quelqu’un pourrais m’aider.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
// Define I2C address
#define I2C_ADDR 0x1C
// Register addresses
#define MAIN_CTRL 0x00
#define ALS_UVS_MEAS_RATE 0x04
#define ALS_UVS_GAIN 0x05
#define ALS_DATA_0 0x0D
#define ALS_DATA_1 0x0E
#define ALS_DATA_2 0x0F
#define UVS_DATA_0 0x10
#define UVS_DATA_1 0x11
#define UVS_DATA_2 0x12
// Define WFAC and UV_SENSITIVITY with appropriate values
#define WFAC 1.0 // Remplacez par la valeur correcte du facteur de fenêtre
#define UV_SENSITIVITY 2300 // Remplacez par la valeur correcte de la sensibilité aux UV
// Function to initialize I2C communication
int init_i2c(char *dev, int addr) {
int file;
if ((file = open(dev, O_RDWR)) < 0) {
perror("Failed to open the bus.");
exit(1);
}
if (ioctl(file, I2C_SLAVE, addr) < 0) {
perror("Failed to acquire bus access and/or talk to slave.");
exit(1);
}
return file;
}
// Function to write data to a register
void write_register(int file, uint8_t reg, uint8_t value) {
if (write(file, ®, 1) != 1) {
perror("Failed to write to the I2C bus.");
exit(1);
}
if (write(file, &value, 1) != 1) {
perror("Failed to write to the I2C bus.");
exit(1);
}
}
// Function to read data from a register
uint16_t read_register(int file, uint8_t reg) {
uint8_t buf[2];
if (write(file, ®, 1) != 1) {
perror("Failed to write to the I2C bus.");
exit(1);
}
if (read(file, buf, 2) != 2) {
perror("Failed to read from the I2C bus.");
exit(1);
}
return (buf[1] << 8) | buf[0];
}
int main() {
char *device = "/dev/i2c-1"; // I2C device file
int file;
// Initialize I2C communication
file = init_i2c(device, I2C_ADDR);
// Configure registers for ALS/UVS measurements
write_register(file, MAIN_CTRL, 0x00); // Disable sensor during setup
write_register(file, ALS_UVS_MEAS_RATE, 0x22); // Set measurement rate and resolution for ALS/UVS
write_register(file, ALS_UVS_GAIN, 0x01); // Set ALS/UVS analog gain range
// Enable sensor for measurements
write_register(file, MAIN_CTRL, 0x01);
// Read ALS and UV data from registers
uint16_t als_data = read_register(file, ALS_DATA_0);
uint16_t uvs_data = read_register(file, UVS_DATA_0);
// Read gain and integration time from registers
uint8_t gain = read_register(file, ALS_UVS_GAIN);
uint8_t integration_time = read_register(file, ALS_UVS_MEAS_RATE);
// Calculate Lux and UVI values
float lux, uvi;
if (gain * integration_time != 0) {
lux = ((0.6 * als_data) / (gain * integration_time)) * WFAC; // Calculate Lux value
} else {
printf("Error: Division by zero in Lux calculation.\n");
}
uvi = (uvs_data / UV_SENSITIVITY) * WFAC; // Calculate UVI value
// Print Lux and UVI values
printf("Lux: %.2f\n", lux);
printf("UVI: %.2f\n", uvi);
// Close I2C communication
close(file);
return 0;
}
EDIT: Mise en forme du programme pour plus de lisibilité.
Impossible de vous aider sans connaître ce qui ne fonctionne pas et/ou les erreurs retournées.
A partir de quelles sources avez vous écrit ce programme ? Avez-vous suivi un tuto ?
Comment est-il branché à l’I2C ? L’I2C est-il démarré sur le Raspberry pi (menu raspi-config) ?