Skip to content

Instantly share code, notes, and snippets.

@futureshocked
Created July 30, 2019 01:12
Show Gist options
  • Save futureshocked/2ae56701e93e9dbc161908b3bda86d19 to your computer and use it in GitHub Desktop.
Save futureshocked/2ae56701e93e9dbc161908b3bda86d19 to your computer and use it in GitHub Desktop.
Grove - Project 3: Make an environment monitor
/* 9.40 - Grove - Project 3: Make an environment monitor
This sketch implements an environment monitor.
It measures and calculates the air temperature using the Grove temperature module (thermistor).
It measures the ambient light using a photoresistor.
It displays the calculated temperature and measured light intensity in the Grove LCD.
If the light intensity is below a certain value, it turns on an LED strip via a Grove relay.
*** A challenge for you: ***
Can you adjust the circuit and sketch so that the value below which the LED strip is turned on can
be adjusted using a potentiometer, instead of being a fixed value?
Components
----------
- Grove Base Shield
- An Arduino Uno compatible board (such as Arduino/Genuino Uno or Seeeduino)
- Grove LCD RGB Backlight module
- Grove - momentary button or Grove touch sensor
- Grove - LED buzzer
- Three Grove cables
- Optional: a mounting piece of cardboard with nylon nuts, bolts and spacer.
IDE
---
Arduino IDE
Libraries
---------
- Wire.h (part of the Arduino IDE)
- rgb_lcd.h (Get it from https://github.com/Seeed-Studio/Grove_LCD_RGB_Backlight)
Connections
-----------
- Use a Grove cable to connect the button or touch sensor module to Grove connector D4.
- Use a Grove cable to connect the buzzer module to Grove connector D8.
- Use a Grove cable to connect the potentiometer module to Grove connector A0.
Other information
-----------------
_ Use this sketch along side the video lecture 09.40 of Grove For Busy People
- Grove LCD RGB Backlight: https://txplo.re/0c9fb
- Grove temperature sensor module: https://txplo.re/4hc
- Grove relay module: https://txplo.re/7fb98
- Grove light sensor module: https://txplo.re/35e13
Github Gist
-----------
<script src="https://gist.github.com/futureshocked/2ae56701e93e9dbc161908b3bda86d19.js"></script>
https://gist.github.com/futureshocked/2ae56701e93e9dbc161908b3bda86d19
For course information, please go to https://techexplorations.com/product/grove-for-busy-people/
Created on July 9 2019 by Peter Dalmaris
*/
#include <Wire.h>
#include "rgb_lcd.h"
#include <math.h>
rgb_lcd lcd;
const int B = 4275; // B value of the thermistor
const int R0 = 100000; // R0 = 100k
const int pinTempSensor = A1; // Grove - Temperature Sensor pin
const int pinLightSensor = A0; // Grove - Light sensor pin
const int pinRelay = 3; // Grove - Relay pin
// A custom character for the degree symbol
byte degree[8] = {
0b00100,
0b01010,
0b01010,
0b00100,
0b00000,
0b00000,
0b00000,
0b00000
};
void setup()
{
pinMode(pinRelay, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.createChar(0, degree);
lcd.clear();
lcd.setCursor(0, 0);
lcd.write("Starting... ");
delay(1000);
}
void loop()
{
int tempSensorValue = analogRead(pinTempSensor);
int lightSensorValue = analogRead(pinLightSensor);
float tempValue = tempCalculation(tempSensorValue);
temperature(tempValue);
light(lightSensorValue);
control_relay(lightSensorValue);
delay(1000);
}
float tempCalculation(int a) {
float R = 1023.0 / a - 1.0;
R = R0 * R;
return 1.0 / (log(R / R0) / B + 1 / 298.15) - 273.15; // convert to temperature via datasheet
}
void light(int sensor_value) {
Serial.print("Light = ");
Serial.println(sensor_value);
lcd.setCursor(0, 1);
lcd.print("Lght: ");
lcd.setCursor(6, 1);
lcd.print(" ");
lcd.setCursor(6, 1);
lcd.print(sensor_value);
}
void temperature(float sensor_value) {
Serial.print("temperature = ");
Serial.println(sensor_value);
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(6, 0);
lcd.print(" ");
lcd.setCursor(6, 0);
lcd.print(sensor_value);
lcd.setCursor(11, 0);
lcd.write((unsigned char)0);
lcd.print("C");
}
void control_relay(int sensor_value)
{
if (sensor_value < 300)
{
lcd.setCursor(10, 1);
lcd.print("ON");
digitalWrite(pinRelay, HIGH);
}
else
{
lcd.setCursor(10, 1);
lcd.print("OFF");
digitalWrite(pinRelay, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment