Note: this page refers to a product that is retired.

EsploraLedShow2

Use the microphone, linear potentiometer, and light sensor on the Esplora to change the color of the onboard LED.

Led Show 2

This example shows you how to read different analog sensors to change the brightness of the three colors in the RGB LED.

In this example you will read the amplitude from the microphone, the amount of light on the light sensor, and the position of the linear potentiometer. These three sensors will control the intensity of the red, blue, and green colors of the RGB LED.

Hardware Required

  • Arduino Esplora

Circuit

Only your Arduino Esplora is needed for this example.

microphone, slider, light sensor and RGB led on the Esplora
microphone, slider, light sensor and RGB led on the Esplora

Code

The RGB LED is comprised of three colors that represent the three primary colors: red, green, and blue. Each of these light's brightness is individually controllable with functions in the Esplora library:

If you want to control all the colors with one instruction you can use the writeRGB() function.

In this example we use three of the sensors on the Esplora to control the brightness of the colors of the LED. The amplitude picked up by the microphone will increase the brightness of the red LED. The position of the slider on the linear potentiometer will change the brightness of the green LED. The intensity of the light on the light sensor will change the brighntess of the blue LED.

1/*
2
3 Esplora Led/Microphone
4
5 This simple sketch reads the microphone, light sensor, and slider.
6
7 Then it uses those readings to set the brightness of red, green and blue
8
9 channels of the RGB LED. The red channel will change with the loudness
10
11 "heard" by the microphone, the green channel changes as the
12
13 amount of light in the room and the blue channel will change
14
15 with the position of the slider.
16
17 Created on 22 november 2012
18
19 By Enrico Gueli <enrico.gueli@gmail.com>
20
21 Modified 24 Nov 2012
22
23 by Tom Igoe
24
25*/
26
27#include <Esplora.h>
28
29void setup() {
30
31 // initialize the serial communication:
32
33 Serial.begin(9600);
34}
35
36int lowLight = 400; // the light sensor reading when it's covered
37int highLight = 1023; // the maximum light sensor reading
38int minGreen = 0; // minimum brightness of the green LED
39int maxGreen = 100; // maximum brightness of the green LED
40
41void loop() {
42
43 // read the sensors into variables:
44
45 int mic = Esplora.readMicrophone();
46
47 int light = Esplora.readLightSensor();
48
49 int slider = Esplora.readSlider();
50
51 // convert the sensor readings to light levels:
52
53 byte red = constrain(mic, 0, 255);
54
55 byte green = constrain(
56
57 map(light, lowLight, highLight, minGreen, maxGreen),
58
59 0, 255);
60
61 byte blue = slider / 4;
62
63 // print the light levels (to see what's going on):
64
65 Serial.print(red);
66
67 Serial.print(' ');
68
69 Serial.print(green);
70
71 Serial.print(' ');
72
73 Serial.println(blue);
74
75 // write the light levels to the LED.
76
77 // note that the green value is always 0:
78
79 Esplora.writeRGB(red, green, blue);
80
81 // add a delay to keep the LED from flickering:
82
83 delay(10);
84}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.