Tinkster Logo
advancedSingle Board Computers (SBCs)

Volume Control on Raspberry Pi with IR Remote Control

Author
Savva
Tashkent, UZ
2 days
--
1
Cover
Friends, after installing Alexa on my Raspberry Pi 3b (hereinafter RPi), the idea arose to control the volume, since I often ask Alexa to play music. I don't have a good microphone yet, and Alexa simply doesn't hear my command to stop when the music is loud. But even if there were a microphone, it wouldn't solve the problem of decreasing and increasing the volume, as AlexaPi does not support such functionality. Also, when someone calls me on the phone, I can't turn down the volume and have to physically disconnect power to the RPi. This instruction is suitable for all those who would like to control the volume on RPi or execute any other terminal command using an IR remote. At home, I had 3 unnecessary IR remotes lying idle, and I decided to use the smallest one for the RPi. I performed this setup on the Raspbian Buster Lite system, kernel version 4.19. So, let's get started.

What you'll need

Materials

Tools

  • Soldering iron1 pc
  • Soldering hot air gun1 pc

Steps

1

Connect the IR receiver to the GPIO interface on the RPi

General purpose input/output diagram on RPi
[object Object]
First, let's set up our IR receiver without mounting it into the RPi case. This IR receiver supports a voltage from 2.5 to 5.5 volts, so it can be powered from a 3.3V or 5V connector. You can use female-to-female jumper wires, but the IR receiver's pins will not fit snugly inside these connectors, so I decided to use male-to-female wires and solder the male ends to the IR receiver's pins for a reliable connection. It's also important to know your remote's frequency before buying an IR receiver, but it's usually 36 or 38kHz. Honestly, I didn't know my IR remote's frequency and bought a second IR receiver at 36kHz just in case, but in the end, it wasn't needed.
Power off the RPi. Next, connect pin #1 on the IR receiver to Ground on the RPi (physical pin #9). Connect pin #2 on the receiver to 3V3 (physical pin #1), and connect pin #3 to GPIO 17 (physical pin #11).
Then, apply power, boot the system, and log into the RPi via SSH from another machine.
1
ssh pi@192.168.0.XXX #Local IP address of RPi
2

Installing LIRC and reading infrared remote control button codes

Installing LIRC and reading infrared remote control button codes
LIRC is an open-source software package that allows receiving and transmitting infrared signals using Linux.
Update packages:
1
sudo apt-get update
Install LIRC:
1
sudo apt-get install lirc
Most likely the installation will result in an error - Failed to start Flexible IR remote input/output application support (Failed to start Flexible IR Remote I/O application support), because some LIRC configuration files have a .dist suffix added and it needs to be removed by moving the files with a new name:
1
sudo mv /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf
2
sudo mv /etc/lirc/lircd.conf.dist /etc/lirc/lircd.conf
Install LIRC again:
1
sudo apt-get install lirc
Next, edit the file lirc_options.conf:
1
sudo nano /etc/lirc/lirc_options.conf
Change the values driver и device to the following:
1
driver = default
2
device = /dev/lirc0
Open the file config.txt in the boot folder:
1
sudo nano /boot/config.txt
Under the line
1
# Uncomment this to enable infrared communication.
add the following values:
1
dtoverlay=gpio-ir,gpio_pin=17
Next, stop LIRC, start it again and check that there are no errors:
1
sudo systemctl stop lircd
2
sudo systemctl start lircd
3
sudo systemctl status lircd
Reboot the system:
1
sudo reboot
After the system reboots, we need to test the IR remote.
Stop LIRC:
1
sudo systemctl stop lircd
and enter the following command:
1
sudo mode2 -d /dev/lirc0
Point our IR remote at the IR receiver and press any buttons on the remote. You should see something similar:
1
Using driver default on device /dev/lirc0
2
Trying device: /dev/lirc0
3
Using device: /dev/lirc0
4
Running as regular user pi
5
space 6169135
6
space 1853594
7
pulse 9020
8
space 4480
9
pulse 590
10
space 538
11
pulse 568
12
space 539
13
pulse 598
14
space 532
15
pulse 594
16
space 538
17
pulse 568
18
space 565
19
.....
If you observe a similar picture, it means the system sees your remote.
Exit this mode by pressing CTRL+Z.
All your IR remote configuration files with the extension .conf should be placed in the folder /etc/lirc/lircd.conf.d/. This folder already contains the file devinput.lircd.conf, but in this setup we won't need it and we will hide it from LIRC by simply moving it with a new extension:
1
sudo mv /etc/lirc/lircd.conf.d/devinput.lircd.conf /etc/lirc/lircd.conf.d/devinput.lircd.conf.copy
Next, we need to read the key codes of our IR remote. In this setup, I used 4 buttons: VOL+, VOL-, a green button for turning sound on/off, and a red button for rebooting the RPi.
Read the codes using the program irrecord. I'll say right away, the program is very buggy and I had to find out the codes for my buttons in two stages.
Go to the home folder:
1
cd ~
Enter the following command and press Enter
1
sudo irrecord -d /dev/lirc0 -H default -f
When the program outputs:
1
Press RETURN to continue
Press again Enter.
Next, after
1
Enter name of remote (only ascii, no spaces)
enter the remote name. In my case alexapi, you will have your own name. Press Enter.
Then the program will output the following message:
1
Now start pressing buttons on your remote control.
2
3
It is very important that you press many different buttons randomly
4
and hold them down for approximately one second. Each button should
5
generate at least one dot but never more than ten dots of output.
6
Don't stop pressing buttons until two lines of dots (2x80) have
7
been generated.
8
9
Press RETURN now to start recording.
After pressing Enter you need to press keys on the remote in any order until at least one dot appears in the terminal. I pressed each key until 2 dots appeared.
Next, the program will output the message:
1
Please enter the name for the next button (press <ENTER> to finish recording)
You need to enter the registered button names. In my case, these were the names KEY_VOLUMEUP, KEY_VOLUMEDOWN, KEY_MUTE и KEY_POWER.
If you want to know all registered button names in irrecord, then before the button recording procedure, you can enter the command and find out all the names:
1
irrecord --list-namespace
After entering the button name, press Enter the program will ask you to press the corresponding button on the remote:
1
Now hold down button "KEY_VOLUMEUP".
Next, the program will ask you to enter the next button name:
1
Please enter the name for the next button (press <ENTER> to finish recording)
If you have already entered all buttons, simply press Enter
The program will save the data to a file and say:
1
Successfully written config file alexapi.lircd.conf
Open this file:
1
nano alexapi.lircd.conf
and see its structure
1
begin remote
2
name alexapi
3
flags RAW_CODES|CONST_LENGTH
4
eps 30
5
aeps 100
6
gap 108034
7
begin raw_codes
8
name KEY_VOLUMEUP
9
9018 4467 595 537 621 511
10
575 531 597 536 623 513
11
573 530 597 535 624 508
12
567 1675 623 1620 596 1672
13
572 1670 621 1622 600 1668
14
577 1665 622 1620 594 538
15
622 1620 593 1675 572 1670
16
617 1625 600 532 627 505
17
570 536 602 1666 568 538
18
604 527 621 511 572 533
19
596 1673 571 1671 626 1616
20
597
21
name KEY_VOLUMEDOWN
22
.....
23
name KEY_MUTE
24
.....
25
name KEY_POWER
26
.....
27
end raw_codes
28
end remote
In this case, we captured button codes in "raw" form. They can be left as is, but I preferred to convert them to 32-bit values. It wasn't possible to make them 32-bit immediately because irrecord works very incorrectly on Raspbian Buster Lite, and after numerous attempts, configuration files with empty button code values were constantly created - 0x0. That's why I "grabbed" button codes in two stages, as this scheme eventually worked.
Close the file and enter the command:
1
sudo irrecord -d /dev/lirc0 -H default -a alexapi.lircd.conf
In the terminal, you should see the new file structure:
1
begin remote
2
name alexapi
3
bits 32
4
flags SPACE_ENC|CONST_LENGTH
5
eps 30
6
aeps 100
7
header 9018 4467
8
one 598 1651
9
zero 598 526
10
ptrail 597
11
gap 108034
12
toggle_bit_mask 0x0
13
frequency 38000
14
begin codes
15
KEY_VOLUMEUP 0xXXXXXXXX
16
KEY_VOLUMEDOWN 0xXXXXXXXX
17
KEY_MUTE 0xXXXXXXXX
18
KEY_POWER 0xXXXXXXXX
19
end codes
20
end remote
Copy the new content from the terminal and open the filealexapi.lircd.conf:
1
nano alexapi.lircd.conf
Delete all lines from the file - CTRL+K and insert the new data.
Save the file and move it to the folder /etc/lirc/lircd.conf.d/:
1
sudo mv alexapi.lircd.conf /etc/lirc/lircd.conf.d/ alexapi.lircd.conf
Starting LIRC:
1
sudo systemctl start lircd
and testing our buttons by entering the command:
1
irw
Now try pressing the volume up button and you should see something like:
1
0000000000ff7887 00 KEY_VOLUMEUP alexapi
Do the same for all buttons from which you "grabbed" the codes.
If you see output values after pressing buttons on the IR remote, this means that the codes of the pressed buttons are correctly received by the IR receiver and recognized by LIRC.
3

Assigning commands to IR remote buttons

Next we need to create two bash files, the first will be responsible for increasing the volume, and the other for decreasing.
Go to /usr/bin/
1
cd /usr/bin
Create the volup file:
1
sudo nano volup
And insert the following into it:
1
#!/bin/bash
2
sudo amixer set PCM -- $[$(amixer get PCM|grep -o [0-9]*%|sed 's/%//')+5]%
In this case, the volume will increase by 5% with each press of the corresponding button. You can set your own values in percent.
Save the file and make it executable:
1
sudo chmod +x volup
Next, create the voldown file:
1
sudo nano voldown
and insert the following into it:
1
#!/bin/bash
2
sudo amixer set PCM -- $[$(amixer get PCM|grep -o [0-9]*%|sed 's/%//')-5]%
Make it executable:
1
sudo chmod +x voldown
Next, rename the file /etc/lirc/irexec.lircrc.dist to /etc/lirc/irexec.lircrc by simple moving:
1
sudo mv /etc/lirc/irexec.lircrc.dist /etc/lirc/irexec.lircrc
Open the file irexec.lircrc:
1
sudo nano /etc/lirc/irexec.lircrc
Erase all lines, CTRL+K and insert the following into it:
1
begin
2
prog = irexec
3
remote = alexapi
4
button = KEY_POWER
5
config = sudo shutdown -r now
6
end
7
8
begin
9
prog = irexec
10
remote = alexapi
11
button = KEY_VOLUMEUP
12
config = sudo volup
13
end
14
15
begin
16
prog = irexec
17
remote = alexapi
18
button = KEY_VOLUMEDOWN
19
config = sudo voldown
20
end
21
22
begin
23
prog = irexec
24
remote = alexapi
25
button = KEY_MUTE
26
config = sudo amixer set PCM toggle
27
end
Save the file and then run the command:
1
/usr/bin/irexec /etc/lirc/irexec.lircrc
Try pressing the red POWER button on the IR remote, the system should reboot. This means everything is working and after the system reboots, you need to make irexec a system process so that it starts automatically when the system starts:
1
sudo systemctl enable irexec
Reboot the system again and your IR remote will be ready to use. Similarly, you can program any buttons on the remote from which you 'grabbed' the codes and assign them terminal commands in the file irexec.lircrc
4

Installing the IR receiver in the RPi case

Installing the IR receiver in the RPi case
Installing the IR receiver in the RPi case
In this case, it is the choice of each individual user where they want to install their IR receiver, but I mounted it on the top cover of the RPi case.
You will need to desolder the wires from the IR receiver contacts, then mark three holes according to the contact arrangement and drill them with a 1mm drill bit. The IR port can be glued to the case with glue ABRO Epoxy Steel Its setting time is 10 minutes. I also glued the contacts on the reverse side for a more secure bond with the case. Next, cut pieces of heat shrink tubing, put them on the wires, solder them to the terminals of the IR receiver according to the diagram, place the heat shrink tubing over the soldering points, and shrink them with a soldering heat gun.
Next, assemble the RPi case, install it in the desired location, and apply power.
Now you can control the sound from your RPi remotely!
I hope you liked this instruction and can apply this knowledge in your own RPi projects.

Discussion (0)

No comments yet. Be the first!

Maker

Avatar
Savva
Tashkent, UZ

Anton is the Managing Partner of Tinkster. He studied oil and gas engineering in the United States and also holds two honors degrees from Tomsk Polytechnic University.