Hooking up Speakers to the Arduino.
It is possible to use the Arduino to generate sound. Of course, the quality of the sound generated by the Arduino chip is mediocre because it only has an 8-bit processor. There are two main ways that the Arduino can be used to generate sound:
- Using analogOutput() with the standard library
- Using functions from the tones library
If you hook up a speaker to an analogOutput, then the change in analog output will only change the volume. The reason being that the analogOutput function changes the duty cycle, but not the frequency – the only thing that changes is the length of time between the generation of each sound.
2. Using functions from the tones library
Using the tone library we can set-up any analog sensor to modulate the frequency of sound generated by the Arduino. To use this library, you must first download and install it from here. To install it just save these files to libraries folder in your sketchbook folder. Once installed this library enables you to easily play a wide variety of musical notes (here is a list of the supported notes).
Since analog sensors are limited to generating input between 0 – 1023, one must use the map function to convert the analog input to support a wider range frequencies widely. In class we mapped the output range to between 20 and 15000, as this is roughly the range that human beings can hear distinguish.
Hooking up a Servo Motor to the Arduino.
Servo motors require a lot of current (amps). Therefore, we always need to run it using the direct 5v power supply rather than attempt to power the motor directly from the Arduino pin as we have done with other output components (e.g. LEDs). There are two ways to use the Arduino to control a servo motor:
- using digitalOutput() with the standard library
- using the servo library (more common approach)
Servo motors are always controlled using digital output. The length of the digital pulse that is delivered by the microcontroller determines the extent to which the motor turns (that is why we are able to use a digital pin to control what seems to be an analog function).
Each servo motor model has its own mapping of pulse-length to turn-ratio. Therefore, we always need to test servo motors to determine the appropriate mapping. Another important consideration is the need to make sure that each pulse is separated so that the servo motor can accurately read the length of each pulse.
(2) Using the servo library
The servo library is a standard Arduino library so there is no need to download it from the internet. That said, we still need to include the library into our sketches - below I provide an overview of how to add a library to an Arduino sketch. When using this library, the commands to control the servo motor are greatly simplified.
Adding and Using Libraries
When we use functionality from an extension library we need to add the library to our sketch by using the "#include" command at the top of our code.
It is important to keep in mind that different libraries are sometimes incompatible. For example, it is troublesome to use the servo and tones library together because they both attempt to control the Arduino timer. Some libraries do work well together, so you just need to make sure to check for compatibility before tearing out your hair because your sketch is not working.
Miscellaneous
Changing the Top Level of the Range for Arduino: the aRef pin on the Arduino board enables one to change the standard voltage level that Arduino uses as the top of the range for measuring analog input (e.g. if we wanted to bring it down from 5v to 3v).
Interesting/Cool Website Links: The Flying Pig is a UK site that has a lot of diagrams of various different movements, motions and mechanisms. It is a great resource when trying to figure out what kind of motor would be best for specific endeavors.
No comments:
Post a Comment