Arduino Output

libraries in arduino

Libraries in Arduino are extra “packs” that you can load to open up more functions to work with, e.g. a library connected to a specific hardware element. Some elements require specific libraries in order for the setup to work.

To see what libraries you have and to add extra ones, go to - Sketch - Include library - Manage libraries -. You can search for libraries, and they will show up with name and description, and you can then install it from here.

If you need a special library which can’t be found here, sometimes they can be found as .ZIP files online directly from the producer or from open source networks. Once downloaded, you can go to - Sketch - Include library - Add .ZIP library - and import the file.

Once your library is downloaded, go to - Sketch - Include library - and chose the library you need, and it will appear with #include <NameOfLibrary.h> (and possibly a selection of other necessary elements to run the library) in the very top of the sketch, and it is ready to use!





Read more about libraries in Arduino here: https://www.arduino.cc/en/reference/libraries

Outputs:

Servo

A servo is a motor with gears and shafts which can be controlled. Depending on the input they are given, they can turn to specific positions within 180 degrees (see normal servo) or rotate in a specific speed and direction within 360 degrees (see continuous servo).

The Servo is connected with ground to ground, power to 5v and middle pin to a digital pin (in this example, pin 9).

Normal servo

This code (directly from the Servo library in Arduino, name: “Sweep”) works for the normal servo within a 180 degree spectrum, and makes it rotate from 0 to 180 degrees in steps of 1 degree:

#include <Servo.h>

Servo myservo;

int pos = 0;

void setup() {

myservo.attach(9);

}

void loop() {

for (pos = 0; pos <= 180; pos += 1) {

myservo.write(pos);

delay(15);

}

for (pos = 180; pos >= 0; pos -= 1) {

myservo.write(pos);

delay(15);

}

}

Continuous servo

As mentioned, the continuous servo can’t be positioned, but can rotate within a full 360 degree spectrum. To test this, you use a function servo.write(speed) which uses values from 0 to 180 to define the speed and direction, with 0 being full speed counterclockwise, 90 being still, and 180 being full speed clockwise.

The code looks like this:

#include <Servo.h>

Servo myservo;

void setup() {

myservo.attach(9);

}

void loop() {

myservo.write(85);

}

85 means that the rotation is counterclockwise and very slow, as it is just slightly less than 90. Try the same with the speed at 95 for a very slow clockwise rotation, or maybe 150 for a quick clockwise rotation.

Example 1: Servo with potentiometer

#include <Servo.h>


Servo myservo; // create servo object to control a servo


int potpin = 0; // analog pin used to connect the potentiometer

int val; // variable to read the value from the analog pin


void setup() {

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}


void loop() {

val = analogRead(potpin);

val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)

myservo.write(val); // sets the servo position according to the scaled value

delay(15); // waits for the servo to get there

}












You can also go to the Servo library in Arduino and choose the file "Knob". Read more about controlling a servo with a knob here.

In order to scale the values from the potentiometer for the servo, the map() function is used. Read more about the map function here.


Read more about #define here.

Neopixel strip

Neopixels are a simple way to make colorful and dynamic interactions.

Connect ground to ground, power to 5v and DIN (digital input) to a digital pin (in this example we use digital pin 5). Make sure that the direction of the strip is correct (as seen in diagram).

Include the library "FastLED" by Daniel Garcia.

To test the neopixels, use the following code which will make the first three pixels light up:

#include "FastLED.h"

#define NUM_LEDS 4

#define DATA_PIN 5

CRGB leds[NUM_LEDS];

void setup()

{

FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);

}

void loop()

{

leds[0]= CRGB( 255, 0, 0);

leds[1]= CRGB( 0, 255, 0);

leds[2]= CRGB( 0, 0, 255);

FastLED.show();

}


Example 2: Neopixel with button

This example makes a chase on the neopixel strip when the button is pressed:

#include "FastLED.h"

#define NUM_LEDS 8

#define DATA_PIN 4

CRGB leds[NUM_LEDS];

int chasePos = 0;

void setup() {

FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds,NUM_LEDS);

pinMode(3, INPUT_PULLUP);

}

void loop() {

boolean btnPressed = !digitalRead(3);

if (btnPressed == true) {

for (int i = 0; i < NUM_LEDS; i = i + 1) {

leds[i] = CRGB( 0, 0, 0);

}

leds[chasePos] = CRGB( 255, 0, 0);

FastLED.show();

chasePos = chasePos + 1;

if (chasePos >= NUM_LEDS) {

chasePos = 0;

}

}

else {

for (int i = 0; i < NUM_LEDS; i = i + 1) {

leds[i] = CRGB( 0, 0, 0);

}

}


FastLED.show();

delay(100);

}

Example 3: Neopixel with IR sensor

This example will make the neopixel light up according to the distance measured:

#include "FastLED.h"

#define NUM_LEDS 21

#define DATA_PIN 4

int h = 0;

CRGB leds[NUM_LEDS];

void setup() {

Serial.begin(9600);

FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds,NUM_LEDS);

}

void loop() {


for (int i = 0; i < NUM_LEDS; i = i + 1) {

leds[i] = CRGB( 0, 0, 0);

}


h = 0;

int h = analogRead(0);

h = map(h, 30, 700, 0, 20);


for (int i = 0; i < h; i = i + 1) {

leds[i] = CRGB( 255, 0, 0);

}


FastLED.show();

}



Read more about the for loop here. For more information on how to make a chase with LED, look here.

Example 3: Sinus fade

This example makes a fade based on a sinewave

float counter = 0;

float sineSpeed = 50.0f;



#include "FastLED.h"

#define NUM_LEDS 4

#define DATA_PIN 10


CRGB leds[NUM_LEDS];

void setup()

{


FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);

// put your setup code here, to run once:

Serial.begin(9600);

}

void loop()

{

// put your main code here, to run repeatedly:

counter = counter + 1;

float sineValue = (sin(counter / sineSpeed) + 1.0f) * 127.5f;

Serial.println(sineValue);

leds[0] = CRGB( sineValue, sineValue, sineValue);

leds[1] = CRGB( sineValue, sineValue, sineValue);

leds[2] = CRGB( sineValue, sineValue, sineValue);

FastLED.show();

}

Example 4: Neopixel with fadeup and own

This example will make the neopixel fade up and down.

float fader = 0;

boolean fadeUp = true;

float dir = 1;



#include "FastLED.h"

#define NUM_LEDS 4

#define DATA_PIN 10


CRGB leds[NUM_LEDS];




void setup()

{


FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);

// put your setup code here, to run once:

Serial.begin(9600);

}




void loop()

{


fader = constrain(fader + 0.5f*dir,0,255);

if(fader == 255)

{

fadeUp = false;

dir = -1;

}

if(fader == 0)

{

fadeUp = true;

dir = 1;

}

Serial.println(fader);

leds[0] = CRGB( fader, fader, fader);

leds[1] = CRGB( fader, fader, fader);

leds[2] = CRGB( fader, fader, fader);

FastLED.show();


}




Read more about the for loop here. For more information on how to make a chase with LED, look here.

Piezo - as a speaker

As an output, the Piezo works as a buzzer or a simple speaker. Connect power to a digital pin (in this example digital pin 8 is used) and ground to ground – again with a 1 M ohm resistor.

To test the piezo, use this code:

int piezoPin = 8;

int frequency = 1000; //change this to change pitch

int duration = 500; // change this to change duration of sound

void setup() {

}

void loop() {

tone(piezoPin, frequency, duration);

}


Example 4: Piezo speaker with potentiometer

Using the piezo with the potentiometer will allow you to adjust the frequenzy of the tones when turning the nob and altering the resistance of the potentiometer.

Use the following code to read the potentiometer values and translate them into tones:

int piezoPin = 8;

int potPin = 0;

void setup() {

pinMode(piezoPin, OUTPUT);

}

void loop() {

int pitch = analogRead(potPin);

tone(piezoPin, pitch);

}




To read more about the tone() function, look here.

picture from: https://fablab.ruc.dk/makerkit/












Maybe this is the first time you see INPUT_PULLUP. Read more about what it means here.

To see a more detailed example using the DFPlayer and DFRobotDFPlayerMini library, go here.

Mp3 trigger

You can play music or sounds as an output by using a DFplayer (MP3 module) connected to a simple speaker or a piezo (in this example we will use a piezo). The DFplayer has an SD-card on which you can upload the file you want to play.

Connect ground to ground, power to 5v, RX to a digital pin (here D11) and TX to another digital pin (here D10). In order to reduce potential noise, add a 1K resistor on the connection between RX and the Arduino. Connect the wires from the piezo to SPK_1 (+) and SPK_2 (-).

Include the library “DFRobotDFPlayerMini” by DFRobot, and use the following code:

#include <SoftwareSerial.h>

#include "Arduino.h"

#include <DFRobotDFPlayerMini.h>

SoftwareSerial mySoftwareSerial(10, 11);

DFRobotDFPlayerMini myDFPlayer;

boolean lastState = false;

void printDetail(uint8_t type, int value);

void setup() {

mySoftwareSerial.begin(9600);

Serial.begin(9600);

while(!myDFPlayer.begin(mySoftwareSerial)) {

Serial.println(".");

delay(300);

}

myDFPlayer.volume(20);

pinMode(3, INPUT_PULLUP);

myDFPlayer.play(1);

}

void loop() {

}

Note: myDFPlayer.volume() sets the volume, which can go from 0 to 30. myDFPlayer.play() indicates which song to play, where 1 is the first, etc.