Skip to content

Hello World!

Our challenge

Let’s make a small intervention, device, installation, however you want to call it, that uses the inputs and outputs below.

Inputs

Inputs today will be either switches or capacitive sensors.

Switches

Simple switches can have many many shapes!

You can use a simple push button to do really fun stuff!
Remember to use the digitalRead() function on your sketch.

Capacitive Sensor

…capacitive sensing (sometimes capacitance sensing) is a technology, based on capacitive coupling, that can detect and measure anything that is conductive or has a dielectric different from air.

ESP32

ESP32 based boards already have support for capacitive sensing through the arduino-esp32 core. An example can be found here

Prep it

You will need to install the core via Boards Manager - Use the esp32 by espressif for this to work.

Example

First run it with as below and check the serial monitor to check the range of values that it reports when you touch the tin foil or the pin…:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(touchRead(T0));
}

Example

Now you can put a conditional with a threshold in the middle between your min (touched) and your max (not touched). Remember to comment the line where you print the sensor value so you can see the Touching!!! message.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
    // put your main code here, to run repeatedly:
    int sensor = touchRead(T0);

    // Serial.println(sensor);

    if (sensor < 30) { // Note that when we touch, the value is lower!
        Serial.println("Touching!!!");
    } else {
        Serial.println("Nothing there!!!");
    }
}

For other Boards

You just need a resistor (around 1Mohm) and a piece of conductive material like tin foil.

Setup

Courtesy of: arduino.cc

Installing the library

Installing the library

The simplest way to make a capacitive sensor work is using a library.
If you don’t know how to install a Library on Arduino IDE please follow this guide

Search for a library called Capacitive Sensor by Paul Badge and Paul Stoffregen and click install.

Reading the sensor

Try this code example and adapt it tou your needs.

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

void setup() {
   Serial.begin(115200);
}

void loop() {

    long sensor =  cs_4_2.capacitiveSensor(30);
    Serial.println(sensor);

//    if (sensor > 2000) {
//        Serial.println("Touching!!!");
//    } else {
//          Serial.println("Nothing there!!!");
//    }

    delay(10);
}

First run it with the conditional (if…) commented and check the serial monitor to check the range of values that it reports when you touch the tin foil:

Now you can put a conditional with a threshold in the middle between your max (touched) and your min (not touched) values, ej. 2250 for the values in this image. Remember to comment the line where you print the sensor value so you can see the Touching!!! message.

#include <CapacitiveSensor.h>

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

void setup() {
   Serial.begin(115200);
}

void loop() {

    long sensor =  cs_4_2.capacitiveSensor(30);
//     Serial.println(sensor);

    if (sensor > 2250) {
        Serial.println("Touching!!!");
    } else {
        Serial.println("Nothing there!!!");
    }

    delay(10);
}

Outputs

Your led strip is composed of several WS2812 or WS2813 led units, it acts like a networked mesh where the signal is passed to all the leds one by one and you talk to each of them by an address that goes from 0 (first led) to N (the last led).

The wiring:

…Place a 300 to 500 Ohm resistor between the Arduino data output pin and the input to the first NeoPixel.

To control this type of leds, the easiest way is using the Adafruit Neopixel library if you feel this is too simple you can start with the more advanced Fast led library.

Install the library with the normal procedure using Arduino library manager.

To test your leds, copy this simple example and change the LED_PIN acording to your wiring.

#include <Adafruit_NeoPixel.h>

#define LED_PIN    12
#define LED_COUNT 20

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show();
}

void loop() {

  for(int i=0; i<strip.numPixels(); i++) {
    strip.clear();
    strip.setPixelColor(i, 0, 50, 255);
    strip.show();
    delay(20);
  }
}

Warning

Dont try to light more than 20 leds if you are using the Feather as a power source, keep in mind that the leds are very power hungry and you will need a bigger power supply to light more. You can find more info on this subject here.

If you want to see a lot of colors try loading the Adafruit Neopixel library example calle strandtest

References

Global Documentation