Arduino volume controller
Today I will show you how to build a simple volume controller using arduino and serial communication in GNU/Linux, specifically Debian Squeeze.
Arduino provides their own tool for serial communication but isn’t practical for most applications because isn’t customizable, although for debugging is well. Due to a college project I am studying about serial programming in order to establish communication with arduino from a program written in C. So far I am able to send and to retrieve data to and from arduino(duemilanove) and the first thing that came to my mind was to make a volume controller for my speakers :-). I hope you find it interesting!
Materials
- 1 Arduino(tested with Duemilanove)
- 1 potentiometer
- Wires
-
C compilers and ncurses libraries, in debian:
apt-get install gcc libncurses5-dev
Let’s get started
As first step, build the following circuit:
Use the following code for arduino to read the input from the potentiometer.
int input = 2;
void setup()
{9600);
Serial.begin(
}
void loop() {
int value = analogRead(input);
int volumen = min(value/10, 100);
// The C program will read this value to change the volume
Serial.println(volumen); 50);
delay( }
This program reads the value that we indicate with the potentiometer(a value from 0 to 1023), then transform this value to a scale from 0 to 100, the volume of the speakers.
In the next program we establish serial communication with arduino, reads the appropiate values and and then, adjust the volume of the speakers using amixer
.
// Compile with: gcc -Wall -o volume volume.c -lcurses
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(int argc, char** argv)
{char buffer[50];
struct termios tio;
int tty_fd, volumen, i;
char v, ch[2];
char *device = "/dev/ttyUSB0";
0,sizeof(tio));
memset(&tio,0;
tio.c_iflag = 0;
tio.c_oflag =
tio.c_cflag = CS8 | CREAD | CLOCAL;0;
tio.c_lflag = 1;
tio.c_cc[VMIN] = 5;
tio.c_cc[VTIME] =
tty_fd=open(device, O_RDWR | O_NONBLOCK);
cfsetospeed(&tio, B9600);
cfsetispeed(&tio, B9600);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
tcsetattr(tty_fd, TCSANOW, &tio);0;
volumen =
initscr();while (ch[0] !='q') {
1);
read(STDIN_FILENO, ch, if (read(tty_fd,&v,1) > 0) {
if (v == '\n') {
"amixer --quiet set Master %d%%", volumen);
sprintf(buffer,
system(buffer);
"[");
printw(for (i = 0; i < 100; i++) {
if (i < volumen)
"#");
printw(else
".");
printw(
}"] %d%%", volumen);
printw(
refresh();
clear();
0;
volumen = else {
} if (v >= '0' && v <= '9')
10 + (v - '0');
volumen = volumen *
}
}
}
endwin();
close(tty_fd);
return EXIT_SUCCESS;
}
Connect the USB cable from arduino to the computer and execute the previous program and we get:
That is it. Until next time.