
Let’s look at the simple code from Grove – Touch Sensor. The code demonstrates with Grove – Touch Sensor and a LED connected to an Arduino. The LED will turn on when you touch the Grove – Capacitive Touch Sensor Module. If no touching is detected, the LED will turn off.
The simple code will be modified for the ESP8266 used for the Rat Bait Station. Instead of attaching a LED, I’ll use the ESP8266’s onboard red LED (pin 0).
Below is the simple code demonstrated with Arduino from Grove – Touch Sensor website:
http://wiki.seeedstudio.com/Grove-Touch_Sensor/
const int TouchPin=9;
const int ledPin=12;
void setup() {
pinMode(TouchPin, INPUT);
pinMode(ledPin,OUTPUT);
}
void loop() {
int sensorValue = digitalRead(TouchPin);
if(sensorValue==1)
{
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
}
Below is the modified code we shall use later for coding the Rat Bait Station.
Test the code... Start the Arduino IDE up. Then create a new sketch and paste the code below in the Arduino IDE followed by upload:
/*
* Grove – Touch Sensor and ESP8266
*/
// defines pins numbers
const int TouchPin = 16; // Pin for capacitive touch sensor
const int ledPin = 0; // Pin for the onboard red LED
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(TouchPin, INPUT);
pinMode(ledPin, OUTPUT); // Sets digital pin connected to onboard red LED as output
digitalWrite(ledPin, HIGH); // Sets initial state of onboard red LED to off, reverse wired
}
void loop() {
int sensorValue = digitalRead(TouchPin);
if(sensorValue==1)
{
digitalWrite(ledPin, LOW); // Turn on red LED - note the LED of GPIO pin0 is reverse wired
Serial.println("ON"); // so setting the pin LOW will turn the LED on
}
else
{
digitalWrite(ledPin, HIGH); // Turn off red LED - note the LED of GPIO pin0 is reverse wired
Serial.println("OFF"); // so setting the pin HIGH will turn the LED off
}
}
1. The ESP8266 onboard red LED will turn on when we touch the Grove – Capacitive Touch Sensor Module and prints “ON” to the serial.
2. If no touching detected, the red LED will turn off and prints “OFF” to the serial.