Different kinds of output for Arduino
Here we will present some different outputs for Arduino, and some examples on simple setups using the inputs presented on the "Arduino Input" page.
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!
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).
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:
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:
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
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.
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:
Example 2: Neopixel with button
This example makes a chase on the neopixel strip when the button is pressed:
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:
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:
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:
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.