Seeed Studio Grove Sensors Example
Dependencies: mbed DHT DigitDisplay
Fork of STM32_Read_Sensors_Example by
main.cpp
00001 #include "mbed.h" 00002 #include "ADXL345_I2C.h" 00003 #include "DHT.h" 00004 #include "DigitDisplay.h" 00005 00006 00007 //#define LED // Grove LED 00008 //#define ACCEL // SEN04051P Grove - 3-Axis Digital Accelerometer(±16g) 00009 //#define LIGHT_SENSOR // Grove Light Sensor using GL5528 photoresistor 00010 //#define WATER_SENSOR // Grove Water Sensor 00011 //#define GAS_SENSOR // SEN90512P Grove - Gas Sensor(MQ2) 00012 //#define TEMP_SENSOR // Grove Temperature Sensor using TTC03 Thermistor 00013 //#define TEMP_HUMID_SENSOR // Grove Temp/Humidity Sensor (DHT11) 00014 #define DIGIT_DISPLAY // Grove 4-digit display 00015 00016 00017 AnalogIn temp_sensor(A0); 00018 AnalogIn analog_light_sensor_read(A1); 00019 DigitalIn water_sensor(A2); 00020 AnalogIn gas_sensor(A3); 00021 DigitalOut my_led(D7); 00022 ADXL345_I2C accelerometer(D14, D15); 00023 DHT temp_humid_sensor(A0, DHT11); // See DHT.h for other supported sensor types 00024 DigitDisplay DigitalDisplay(D2, D3); // For connection to port D2 on Grove shield 00025 00026 int main() { 00027 00028 float vol; 00029 float Rsensor; 00030 int gas_sensor_value; 00031 int sound_sensor_value; 00032 int adc_scale = 4096; 00033 int readings[3] = {0, 0, 0}; 00034 int sensorValue; 00035 int B = 3975; 00036 float resistance; 00037 float temperature; 00038 float temperature_f; 00039 float humidity; 00040 float dewpoint; 00041 char amb_temp[6]; 00042 int a; 00043 00044 #ifdef ACCEL 00045 printf("Starting ADXL345 test...\n\r"); 00046 wait(.001); 00047 printf("Device ID is: 0x%02x\n\r", accelerometer.getDeviceID()); 00048 wait(.001); 00049 00050 // These are here to test whether any of the initialization fails. It will print the failure 00051 if (accelerometer.setPowerControl(0x00)) 00052 { 00053 printf("didn't intitialize power control\n"); 00054 return 0; 00055 } 00056 00057 //Full resolution, +/-16g, 4mg/LSB. 00058 wait(.001); 00059 00060 if(accelerometer.setDataFormatControl(0x0B)) 00061 { 00062 printf("didn't set data format\n"); 00063 return 0; 00064 } 00065 wait(.001); 00066 00067 //3.2kHz data rate. 00068 if(accelerometer.setDataRate(ADXL345_3200HZ)) 00069 { 00070 printf("didn't set data rate\n"); 00071 return 0; 00072 } 00073 wait(.001); 00074 00075 //Measurement mode. 00076 if(accelerometer.setPowerControl(MeasurementMode)) 00077 { 00078 printf("didn't set the power control to measurement\n"); 00079 return 0; 00080 } 00081 #endif 00082 00083 while(1) 00084 { 00085 #ifdef LED 00086 my_led = 1; // LED is ON 00087 wait(0.2); // 200 ms 00088 my_led = 0; // LED is OFF 00089 wait(1.0); // 1 sec 00090 #endif 00091 00092 #ifdef WATER_SENSOR 00093 printf("water_sensor: %d \n\r", water_sensor.read()); 00094 wait(0.5); 00095 #endif 00096 00097 #ifdef GAS_SENSOR 00098 gas_sensor_value = gas_sensor.read_u16(); 00099 printf("gas_sensor_value: 0x%X \n\r", gas_sensor_value); 00100 00101 vol = (float)gas_sensor_value/adc_scale*5.0; 00102 printf("gas vol: %f \n\r", vol); 00103 wait(1); 00104 #endif 00105 00106 #ifdef ACCEL 00107 accelerometer.getOutput(readings); 00108 wait(0.1); // 100 ms 00109 00110 /* x-axis, y-axis and z-axis */ 00111 printf("%d, %d, %d\n\r", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); 00112 #endif 00113 00114 #ifdef LIGHT_SENSOR 00115 sensorValue = analog_light_sensor_read.read_u16(); 00116 Rsensor=(float)(adc_scale-sensorValue)*10/sensorValue; 00117 printf("Light Sensor Analog Reading is 0x%X = %d ", sensorValue, sensorValue); 00118 printf("The sensor resistance is %f \n\n\r", Rsensor); 00119 wait(1); // 1s 00120 #endif 00121 00122 #ifdef TEMP_SENSOR 00123 a = temp_sensor.read_u16(); 00124 00125 resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor; 00126 temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; //convert to temperature via datasheet ; 00127 temperature_f = (1.8 * temperature) + 32.0; 00128 sprintf(amb_temp, "%0.2f", temperature_f); 00129 00130 printf("Temp Sensor Analog Reading is 0x%X = %d ", a, a); 00131 printf("Current Temperature: %f C %f F \n\r", temperature, temperature_f); 00132 wait(1); // 1s 00133 #endif 00134 00135 #ifdef TEMP_HUMID_SENSOR 00136 temp_humid_sensor.readData(); 00137 00138 temperature = temp_humid_sensor.ReadTemperature(CELCIUS); 00139 temperature_f = temp_humid_sensor.ReadTemperature(FARENHEIT); 00140 humidity = temp_humid_sensor.ReadHumidity(); 00141 dewpoint = temp_humid_sensor.CalcdewPointFast(temperature, humidity); 00142 00143 printf("\r\n"); 00144 printf("Temperature: %f C / %f F\r\n", temperature, temperature_f); 00145 printf("Humidity: %f%%\r\n", humidity); 00146 printf("Dewpoint: %f C / %f F\r\n", dewpoint, (dewpoint * 1.8f) + 32); 00147 wait(1); 00148 #endif 00149 00150 #ifdef DIGIT_DISPLAY 00151 static int16_t message[] = {0xF, 0xE, 0xE, 0xD, 0xC, 0x0, 0xD, 0xE}; 00152 static int ArraySize = sizeof(message)/sizeof(message[0]); 00153 static int MessageStart = 0; 00154 DigitalDisplay.on(); 00155 00156 for (int position = 0; position <= 3; position++) 00157 { 00158 DigitalDisplay.write(position, message[(position + MessageStart) % ArraySize]); 00159 } 00160 MessageStart = (MessageStart + 1) % ArraySize; 00161 00162 wait(.3); 00163 #endif 00164 00165 } 00166 }
Generated on Tue Jul 19 2022 20:42:14 by 1.7.2