Arduino MAKERKIT
The kit is a collection of components that enables you to make a wide array of experiments.
The Arduino board
Arduino Uno is a microcontroller board.
ATmega328P Chip
16 MHz
Flash Memory 32 KB
14 digital input/output pins (6 PWM out)
Max 20 mA per pin
6 analog inputs,
USB connection
Power jack (6-20V)
Reset button
Test led
You can tinker with your Uno without worrying too much about doing something wrong, worst case scenario you can replace the chip for a few dollars and start over again.
Arduino shield
The shield allows you to quickly connect different components to the Arduino board. Notice how there are multiple 5v pins and ground pins. This enables you to have more than one component connected at the same time.
The white rows (digital and analogue) correlate to the pins on the Arduino board. So the white 0 pin is connected to pin 0 on the Arduino (see yellow illustration).
Blink test
On the board there is a LED connected to pin 13. We can make it blink with:
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Serial print
Prints the time in milliseconds to the serial monitor every 3 seconds.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("starting");
}
void loop() {
Serial.println(millis());
delay(3000);
}
Open the serial monitor to see the info.
Blink and print over USB
On the board there is a LED connected to pin 13. We can make it blink with:
void setup() {
Serial.begin(9600); //setup USB port for communication
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output.
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("ON"); // Print on serial "ON"
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
Serial.println("OFF"); // print on Serial "OFF"
delay(1000); // wait for a second
}
Open the serial monitor to see the info.
Detect a button state
A push button is a simple way to create interaction. It can be used to create different outputs depending on the length of the push, the number of pushes, etc.
Attach one wire from the button to a digital pin and the other to the ground. To test the circuit, use the following code:
void setup() {
pinMode(5, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
boolean btnPressed = !digitalRead(5);
if(btnPressed == true) {
Serial.println("button has been pressed.");
}
}
Detecting button single press - An Event
boolean lastState = false;
void setup()
{
pinMode(5, INPUT_PULLUP);
Serial.begin(9600);
}
void loop()
{
boolean btnPressed = !digitalRead(5);
if (btnPressed == true && lastState == false)
{
Serial.println("button has been pressed.");
}
lastState = btnPressed;
}
Control neopixels
Neopixels are versatile light strings where each diode can be controlled individually. Each one has Red, Green, and Blue and can be from 0 to 255 in brightness. Multiple libraries exists to use noepixels. We suggest you use fastled.
Add the library: Press the library icon and Search for "fastled" and click install
IMPORTANT: connect 5v to v, Di to s and G to g AND make sure the direction is correct
Turn red, green and blue for the first three pixels:
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 10
#define DATA_PIN 2
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}
void loop() {
// Turn the LED on, then pause
leds[0]= CRGB( 255, 0, 0);
leds[1]= CRGB( 0, 255, 0);
leds[2]= CRGB( 0, 0, 255);
FastLED.show();
}
Other examples
Unfold to find other examples of how you can use neopixels.
Turn 13 leds red:
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 13
#define DATA_PIN 2
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}
void loop() {
for (int i = 0; i < NUM_LEDS; i = i + 1)
{
leds[i] = CRGB( 255, 0, 0);
}
FastLED.show();
}
Make a chase
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 13
#define DATA_PIN 2
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
Serial.begin(9600);
}
int numLedsOn = 0;
void loop() {
for (int i = 0; i < NUM_LEDS; i = i + 1)
{
if (i < numLedsOn)
{
leds[i] = CRGB( 255, 0, 0);
}
else
{
leds[i] = CRGB( 0, 0, 0);
}
}
FastLED.show();
numLedsOn = numLedsOn + 1;
if (numLedsOn == NUM_LEDS)
{
numLedsOn = 0;
}
Serial.println(numLedsOn);
delay(100);
}
Button + neopixels
This example combines a button (input) with the Neopixels (output). Thus, your first interactive installation.
Connect the button to pin 3 and the neopixel to pin 4.
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 10
#define DATA_PIN 4
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
pinMode(3, INPUT_PULLUP);
}
void loop() {
boolean btnPressed = !digitalRead(3);
if (btnPressed == true)
{
leds[0] = CRGB( 255, 0, 0);
leds[1] = CRGB( 0, 255, 0);
leds[2] = CRGB( 0, 0, 255);
}
else
{
leds[0] = CRGB( 0, 0, 0);
leds[1] = CRGB( 0, 0, 0);
leds[2] = CRGB( 0, 0, 0);
}
FastLED.show();
}
Use google color picker to pick colors
Color matters and you can use the google color picker to pick colors. Use the Red, Green and Blue values in your code.
Control a servo
Servos are motors in which their position can be controlled. They have an action radius of 0 to 180 degrees. Some servos are "hacked" to be continuous. They will rotate 360 degrees, but cannot be positioned. They are then able to move in a controlled speed and direction. The orange wire is signal and should be connected to S.
Use pin 5 for the example below and change the "pos" to a number between 0 and 180 to change the angle.
#include <Servo.h>
Servo myservo;
int pos = 90;
void setup() {
myservo.attach(9);
}
void loop() {
myservo.write(pos);
}
Distance sensor
One simple sensor to use is the distance sensor. This is an infrared sensor which receives values from 0-1024 relative to about 15cm to 60cm.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0)); // pin Analog 0
}
Distance + Neopixel
Connect neopixel to pin 2 and connect distance sensor to A0.
Two leds strings example
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 10 // max length of the two led strings
#define DATA_PIN1 10
#define DATA_PIN2 11
// Define the array of leds
CRGB leds[2][NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN1>(leds[0], NUM_LEDS);
FastLED.addLeds<NEOPIXEL, DATA_PIN2>(leds[1], NUM_LEDS);
}
void loop() {
// Turn the LED on leds R, G, B on string one
leds[0][0] = CRGB(255, 0, 0);
leds[0][1] = CRGB(0, 255, 0);
leds[0][2] = CRGB(0, 0, 255);
// Turn the LED on 3 leds red on string two
leds[1][0] = CRGB(255, 0, 0);
leds[1][1] = CRGB(255, 0, 0);
leds[1][2] = CRGB(255, 0, 0);
FastLED.show();
}
Control brightness
#include "FastLED.h"
// How many leds in your strip?
#define NUM_LEDS 13
#define DATA_PIN 2
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}
void loop() {
int brigthness = map(analogRead(A0), 0,700,0,255);
for (int i = 0; i < NUM_LEDS; i = i + 1)
{
leds[i] = CRGB( brigthness, brigthness, brigthness);
}
FastLED.show();
}
Turn on and off based on distance
#include "FastLED.h"
#define NUM_LEDS 12
#define DATA_PIN 2
CRGB leds[NUM_LEDS];
int irPin = A0;
void setup()
{
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
Serial.begin(9600);
}
void loop()
{
Serial.println(analogRead(irPin));
if (analogRead(irPin) > 200 && analogRead(irPin) < 500)
{
leds[0] = CRGB( 7, 245, 201);
leds[1] = CRGB( 7, 245, 201);
leds[2] = CRGB( 7, 245, 201);
}
else
{
leds[0] = CRGB( 0, 0, 0);
leds[1] = CRGB( 0, 0, 0);
leds[2] = CRGB( 0, 0, 0);
}
FastLED.show();
}
Two point connection sensing
By using two “antennas” as sender and receiver, you can detect touch in between the two points.
Connect one sensor to A0 and the other to A1, and use the following code:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(touchRead(A0, A1));
Serial.print(",");
// add more touch points
/*
Serial.print(touchRead(A0, A2));
Serial.print(",");
Serial.println(touchRead(A1, A2));
*/
Serial.println("");
}
float touchRead(int pin1, int pin2) {
float smoothValue = 0;
pinMode(pin1, OUTPUT);
pinMode(pin2, INPUT);
for (int i = 0; i < 100; i++) {
digitalWrite(pin1, HIGH);
delayMicroseconds(5);
int high = analogRead(pin2);
digitalWrite(pin1, LOW);
delayMicroseconds(5);
int low = analogRead(pin2);
smoothValue = smoothValue * 0.9f + (float)(high - low) * 0.1f;
}
return (float)smoothValue;
}
Using two analogue pins you can sense the connection between two metal poles. E.g. you can make installations where people needs to hold hands to trigger something.
Joystick
You can connect a joystick to get analog and digital input.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(0));
}
Joystick + servo
#include <Servo.h>
Servo myservo;
void setup() {
myservo.attach(5); // attaches the servo on pin 5 to the servo object
}
void loop() {
myservo.write(map(analogRead(0), 0, 1024, 0, 180));
}
Timing without delay
Using delay is bad practice because it halts everything on the board. Instead use "millis()". This example does something every two seconds
unsigned long timer = 0;
void setup()
{
}
void loop()
{
if(millis()-timer > 2000) // do something every two seconds)
{
timer = millis();
//do something here
}
}
MP3 Player
Use the MP3 player to make interactive sounds for voice commands and games.
Remember to install the library like above:
Simple sample code:
#include <SoftwareSerial.h>
#include "Arduino.h"
#include <DFRobotDFPlayerMini.h>
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX - mp3
DFRobotDFPlayerMini myDFPlayer;
boolean lastState = false;
void printDetail(uint8_t type, int value);
void setup() {
mySoftwareSerial.begin(9600); //for mp3 player
Serial.begin(9600);
while(!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(".");
delay(300);
}
myDFPlayer.volume(20); //Set volume value. From 0 to 30
pinMode(3, INPUT_PULLUP);
myDFPlayer.play(1); //Play the first mp3
}
void loop() {
}
MPU6050: Sensing movement and direction
The MPU is a version of an IMU (inertia measurement unit) sensor, which can measure three-dimensional movement and positions. The MPU is a combination of an accelerometer (measures gravitational acceleration) and a gyroscope (measures rotational velocity).
Install the library “MPU6050” by Adafruit – it might ask you to install a series of other libraries in order for this to run.
Go to – File – Examples – Adafruit MPU6050 – and chose the example called “plotter” in order to read the values from the accelerometer in the serial plotter.
The plotter-example looks like this:
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
void setup(void) {
Serial.begin(115200);
while (!Serial) {
delay(10);
}
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("");
delay(100);
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
Serial.print(a.acceleration.x);
Serial.print(",");
Serial.print(a.acceleration.y);
Serial.print(",");
Serial.print(a.acceleration.z);
Serial.print(", ");
Serial.print(g.gyro.x);
Serial.print(",");
Serial.print(g.gyro.y);
Serial.print(",");
Serial.print(g.gyro.z);
Serial.println("");
delay(10);
}
MPU: GY-85
There are different kinds of IMU - For the GY-85 you do the same wiring but use GY-85 library. You need to do the following:
1.Download the zip file from Github
2. Add Zip in the Arduino menu
Open the example in the example
if the example does not show up in the example menu then it is the same that is on the right here and you can just copy paste it.
#include "GY_85.h"
#include <Wire.h>
GY_85 GY85; //create the object
void setup()
{
Wire.begin();
delay(10);
Serial.begin(9600);
delay(10);
GY85.init();
delay(10);
}
void loop()
{
int ax = GY85.accelerometer_x( GY85.readFromAccelerometer() );
int ay = GY85.accelerometer_y( GY85.readFromAccelerometer() );
int az = GY85.accelerometer_z( GY85.readFromAccelerometer() );
int cx = GY85.compass_x( GY85.readFromCompass() );
int cy = GY85.compass_y( GY85.readFromCompass() );
int cz = GY85.compass_z( GY85.readFromCompass() );
float gx = GY85.gyro_x( GY85.readGyro() );
float gy = GY85.gyro_y( GY85.readGyro() );
float gz = GY85.gyro_z( GY85.readGyro() );
float gt = GY85.temp ( GY85.readGyro() );
Serial.print ( "accelerometer" );
Serial.print ( " x:" );
Serial.print ( ax );
Serial.print ( " y:" );
Serial.print ( ay );
Serial.print ( " z:" );
Serial.print ( az );
Serial.print ( " compass" );
Serial.print ( " x:" );
Serial.print ( cx );
Serial.print ( " y:" );
Serial.print ( cy );
Serial.print (" z:");
Serial.print ( cz );
Serial.print ( " gyro" );
Serial.print ( " x:" );
Serial.print ( gx );
Serial.print ( " y:" );
Serial.print ( gy );
Serial.print ( " z:" );
Serial.print ( gz );
Serial.print ( " gyro temp:" );
Serial.println( gt );
delay(500); // only read every 0,5 seconds, 10ms for 100Hz, 20ms for 50Hz
}
Microphone: Get the volume
You can use the little volume circuit to get the volume. I recommend you use the serial plotter in the arduino gui to see the reactions. Turn the little screw on the blue component to change the sensitivity. It should be around 500 when no sound is present.
int soundPin = A0;
float sensorValue = 0; float volume = 0;
void setup ()
{
Serial.begin (9600);
}
void loop ()
{
sensorValue = analogRead (soundPin);
volume = ((float)abs(sensorValue - 670)) * 0.1f + volume * 0.9f;
Serial.print (sensorValue);
Serial.print(",");
Serial.println(volume * 100);
//delay (2);
}
Create a touch sensor
With only one wire you can detect touch. Do the following:
Go to manage libraries
Install ADCTouch by Martin2250
Put one wire in analog zero.
Use the following code:
#include <ADCTouch.h>
float value = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
value = getTouchValue(A0);
Serial.println(value);
delay(100);
}
float getTouchValue(int pin) {
float smooth = 0;
for (int i = 0; i < 100; i = i + 1) {
smooth = smooth * 0.99f + 0.01f *ADCTouch.read(pin,1);
}
return smooth;
}
Go to the serial monitor to see the output (top left corner):
PC Fan control
// Include the library
#include <FanController.h>
#define SENSOR_PIN 2
#define SENSOR_THRESHOLD 1000
#define PWM_PIN 9
FanController fan(SENSOR_PIN, SENSOR_THRESHOLD, PWM_PIN);
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Fan Controller Library Demo");
// Start up the library
fan.begin();
// Set fan duty cycle - a 0-100 range // setting the speed
fan.setDutyCycle(50);
}
/*
Main function, get and show the temperature
*/
void loop(void)
{
// Call fan.getSpeed() to get fan RPM.
Serial.print("Current speed: ");
unsigned int rpms = fan.getSpeed(); // Send the command to get RPM
Serial.print(rpms);
Serial.println("RPM");
// Not really needed, just avoiding spamming the monitor,
// readings will be performed no faster than once every THRESHOLD ms anyway
delay(250);
}
Install the library FanController