6. Input & Output¶
Input Devices¶
Devices used to convert data acquire data from reality and insert them into data that can be used by the microcontroller.
Classification of Known Devices¶
Bigsound - Sound Sensor¶
Bigsound is an analogue sensor that measures the sound waves (intensity) and produces a digital output signal using a microphone, Sound sensors are usually used in security systems, monitoring and speech recognition software, it does operate in the range of 3.3-5 V (Vcc = 5V). more details
Connection - Electric Circuit - Reality¶
Arduino IDE - Code¶
Using the Below Code, Bigsound is expected to recieve sound, provide sound level value in the serial monitor based on the sound pressure it recieves into electrical signal.
const int soundSensorPin = A0; // Analog pin connected to the Big Sound Sensor Module
const int ledPin = 2; // Digital pin connected to the LED (optional)
void setup() {
pinMode(soundSensorPin, INPUT); // Set the Sound Sensor pin as INPUT
pinMode(ledPin, OUTPUT); // Set the LED pin as OUTPUT (optional)
Serial.begin(9600); // Initialize serial communication for debugging (optional)
}
void loop() {
int soundValue = analogRead(soundSensorPin); // Read the analog value from the Sound Sensor
// Display the sound sensor value on the Serial Monitor
Serial.print("Sound Level: ");
Serial.println(soundValue);
// Adjust the threshold value according to your environment
delay(50); // Add a small delay to avoid rapid repeated detections
}
Video - Application¶
Output Devices¶
Micro Servo Motor¶
A motor type that is a rotation movement output device powered by a DC Source, either from an external supply or by a controller, small and lightweight with high power output, this motor is accurate, can define starting and ending angles of rotation with certain speed, it was connected to pin 9 of digital for output, it does operate at 4.8-6V, and move according to the electrical signals provided to it by the microcontroller from the uploaded program code and turns it into kinetic energy in the form of rotational movement, the code can adjust the speed of the micro servo motors rotation and when it starts and stops.
Connection¶
Normal Operating Rotation¶
The Code is taken from Arduino IDE File>Examples>Servo>Sweep, I made some adjustments to fix the speed of Rotation.
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
Fast Rotation¶
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
}
Slow Rotation¶
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15 ms for the servo to reach the position
}
}
Input to Output¶
Connecting the big sound to the micro servo motor to take sound signals and turn them into signal to start micro servo motor Rotation
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
const int soundSensorPin = A0; // Analog pin connected to the Big Sound Sensor Module
const int ledPin = 2; // Digital pin connected to the LED (optional)
void setup() {
pinMode(soundSensorPin, INPUT); // Set the Sound Sensor pin as INPUT
pinMode(ledPin, OUTPUT); // Set the LED pin as OUTPUT (optional)
Serial.begin(9600); // Initialize serial communication for debugging (optional)
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
int soundValue = analogRead(soundSensorPin); // Read the analog value from the Sound Sensor
// Display the sound sensor value on the Serial Monitor
Serial.print("Sound Level: ");
Serial.println(soundValue);
if (soundValue >= 20)
{
for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
}
delay(50); // Add a small delay to avoid rapid repeated detections
}
Issues¶
There was a problem with Bigsound detecting external sound waves unrelative to the experiment (not produced by the user) which cause the servo motor to sometimes move on its own or make the way it works similar to a staged scene.