Tagged: raspberry pi

Script to change keyboard layout on Raspberry Pi

To make it easier to change keyboard layout on a raspberry pi I wrote a small shell script. Be warned, this is one of my first scripts. So it is not optimal, and no guarantees. Use at your own risk.


clear
echo "--------------------------------"
echo "Script to change keyboard layout"
echo "--------------------------------"
echo "Which layout should be used? tr=turkish, se=swedish, gb=english etc"
read layout

sudo sed -i 's|XKBLAYOUT=....|XKBLAYOUT="'$layout'"|g' /etc/default/keyboard

echo "Changed to"
grep "XKBLAYOUT" /etc/default/keyboard | sed 's/XKBLAYOUT=\(....\)/\1/g'
sleep 1
sudo reboot
exit 0

Save the code to a file change_keyboard.sh (or downoad from here). Then add execute permission on that file with the command chmod 755 change_keyboard.sh Then you can run the file with ./change_keyboard.sh

Look in this list to find the 2 character code for the country layout you want to use.

Raspbian keyboard shortcut for window management

Raspberry Pi tip of the day: If you want to maximize your window, but do not have a mouse to click the icon, you can press alt+spacebar to open the menu in the windowbar. Similarly, press alt+[the letter that is underlined] to open one of the the menu’s in the menubar.

How to install Scratch for Arduino (S4A) on Raspberry Pi

Scratch for Arduino is an extension on Scratch for controlling a connected Arduino. It is a great way to start programming and building electronics.

  • First thing to do is start your Raspberry Pi, and make sure it is connected to the internet.
  • Open the browser (Midori) and go to www.s4a.cat.
  • On that page go to Downloads, click on the link that says “Raspbian (Debian for RaspberryPi)” and choose “Save”. This will save the file “S4A15_RP.deb” to /home/pi/.
  • Now open a terminal window, by clicking on LXTerminal on the desktop. In this window you can type commands to make your computer do things. Now we want to tell it to install the file we just downloaded.
  • The location you are at when you open the terminal should be /home/pi/. You can check this by typing the command pwd and pressing enter. It will then show you in which directory you are working.
  • This should also be the place we saved the file. We can check this by typing ls and enter. It will then show you all files and directories inside the directory we are now. In this list there should be a file called “S4A15_RP.deb” or similar.
  • Now let’s install it. For this we will use the command dpkg. But to run this command the computer demands that we have the rights to install this. Therefore we need to add the command sudo to execute the command as a super user. So in the end, the command you need to type becomes: sudo dpkg -i S4A15_RP.deb . It is important to write it with capital letters where needed, because otherwise the computer does not know what file you are talking about.
  • The installation will take a little while, and the computer will show you what it is doing by writing all kinds of text. When it is done (you see pi@raspberrypi ~$ again) you can close the window.

Commands in the terminal

  • Now if you open the start menu and go to Programming, there should be S4A in the list. Click on it to start Scratch for Arduino.
  • Of course we also need an Arduino to program on. This Arduino needs to be prepared for S4A by loading a specific ‘sketch’ (an Arduino code file) onto it. This file can also be found on www.s4a.cat under Downloads (heading ‘Firmware’).
  • If you have access to a computer with the Arduino software already installed, it is easiest to do it from there. Open and upload the file to your Arduino as a regular Arduino code.
  • Note that not all Arduino’s work well with Scratch for Arduino. Currently only the Arduino Diecimila, Duemilanove and Uno are tested.
  • When you start S4A you will see a drawing of an Arduino already loaded as a sprite.
  • It will automatically search for the Arduino. If this does not find it, right click on the sensor board (the list with all the blue values) and select “stop searching board”.
  • Right click it again and click “Select Serial/USB-port”. Then there should pop up a little menu where you can select the port of your connected Arduino.

Analog video (RCA cable) on NOOBS Raspberry Pi

HDMI to VGA adapter
HDMI to VGA adapter

On a Raspberry Pi where the operating system (e.g. Raspbian) is installed using the NOOBS setup, using the analog port for video can give some trouble. In this post I’ll tell you why, and how to fix it.

The Raspberry Pi has quite a few ways to connect it to a screen. The most common one is the HDMI port because screens these days are often equipped with a HDMI connection. As an alternative, a HDMI to VGA adapter can be used to connect a VGA (analog) cable to the HDMI port. The third option is useful for old tv’s that only have a RCA/composite video port (or scart port, using this composite to scart adapter set to input). For the more advanced users you can also connect to the Raspberry using a remote desktop approach, and use for example an Android phone as screen (and mouse!).

RCA/composite video port on a tv
RCA/composite video port on a tv

Unfortunately, when NOOBS was used to install the os, the RCA connector seems to not work. The reason for this is that NOOBS has added a setting to the config.txt file, forcing the Raspberry to output its video on the HDMI port. So the solution for making the RCA port work is to remove that setting. Of course, if you have a HDMI cable or adapter, you can use that to boot the Raspberry, open the file and change the setting (see below for instructions). Otherwise if all you have is an RCA cable, it is easier said than done. As far as I have found, the only option then is to put the SD card into a Linux machine, and change the file that way. As a last resort, you could also give the Raspberry internet, figure out its IP address, and log in to it remotely to change the file.

RCA/Composite cable
RCA/Composite cable

If you have a HDMI cable
This is the easiest way.

  1. Connect the Raspberry via the HDMI port, and start it up.
  2. Open a terminal window (“LXTerminal”).
  3. Type: cd ../../boot to get to the /boot/ directory.
  4. Type: sudo nano config.txt to open the config.txt file with ‘administrator’ rights. If you are asked for a password, write raspberry and press enter. (You will not see anything happening while typing in the password.)
  5. Move down to the line where it says: # NOOBS Auto-generated Settings:
  6. Put a # in front of hdmi_force_hotplug=1 to disable it.
  7. Press Ctrl+x to close the file, and press Enter to save it with the same name.
  8. Now you are done! You can open the file once more to see if the saving worked. (If it did not, you might have forgotten the sudo command.)
  9. Now you can restart the Raspberry with the RCA cable connected instead of the HDMI cable.

If you have a Linux computer

  1. Turn off the Raspberry. Make sure it is off by unplugging the power (usb) cable.
  2. Take the SD card out, and put it in the SD card reader of your computer.
  3. After a short moment you should see a file explorer window open, showing the contents of the contents of the card. If nothing happens, open a file explorer yourself, and locate the SD card with the name BOOT.
  4. In this BOOT directory you should find a file called config.txt. Open it in a text editor.
  5. Scroll down to find the line where it says: # NOOBS Auto-generated Settings:
  6. Put a # in front of hdmi_force_hotplug=1 to disable it.
  7. Save and close the file.
  8. Put the SD card back in the Raspberry and start it up, with the RCA cable connected.

If you have an Android phone, internet cable, and access to your router (and feel a bit adventurous).
There are more ways to do this, feel free to find alternatives to what I write here.

  1. Start up the Raspberry without it being connected to a screen.
  2. Connect the Raspberry with an internet cable to your router.
  3. On your Android phone, find and install the app Ping Tools
  4. Make sure your phone is connected on the wifi of the router. And make sure the Raspberry had some time to start up (1-2 minutes)
  5. Open the app, tap on the top blue bar that says Ping and select Subnet scanner in the dropdown menu.
  6. Click on the settings button (right next to the scan button).
  7. In the list of ports, add the number 22. This is the port that the Raspberry will have open.
  8. Then tap the scan button. Now your phone will try to look for other devices on the same network. This will take a while. If you see a device that has port 22 open, then that is most likely the iIP address of your Raspberry.
  9. Now you can remotely connect to the Raspberry using SSH (Secure Shell). For this you can either download Putty on a computer or use an Android SSH app.
    • On a Computer connected to the same network
      1. Download putty.exe and run it.
      2. Fill in the IP address of the Raspberry in the Hostname field. The port should be 22.
      3. Then click connect.
      4. The first time you will get an alert, click yes.
      5. As username type in pi and the password raspberry.
    • On an Android phone
      1. Download a SSH app, for example ConnectBot.
      2. You probably also need a keyboard that gives you more control, like the Hacker’s keyboard.
      3. Fill in pi@192.168.1.1 where you replace the IP address with the address of your Raspberry.
      4. Type in raspberry as password.
  10. Now that we have a SSH connection to the Raspberry, we can do anything that you can do in a terminal window on the Raspberry.
  11. Type: cd ../../boot to get to the /boot/ directory.
  12. Type: sudo nano config.txt to open the config.txt file with ‘administrator’ rights. If you are asked for a password, write raspberry and press enter. (You will not see anything happening while typing in the password.)
  13. Move down to the line where it says: # NOOBS Auto-generated Settings:
  14. Put a # in front of hdmi_force_hotplug=1 to disable it.
  15. Press Ctrl+x to close the file, and press Enter to save it with the same name.
  16. Now you are done! You can open the file once more to see if the saving worked. (If it did not, you might have forgotten the sudo command.)
  17. Now you can restart the Raspberry with the RCA cable connected instead of the HDMI cable.

If it still does not work
Please note that if the HDMI cable is plugged in, the Raspberry will automatically switch to that as output. So make sure the RCA cable is connected before the Raspberry starts, and no HDMI cable is connected. If it still not works, feel free to ask your questions in the comments!

Raspberry pi on-screen keyboard

Because I don’t have any usb keyboard at home, other than my big and bulky laptop stand, it is a bit of a challenge to type on a Raspberry Pi. I’ve even went as far as copying letters one by one with a mouse to type something.

But now that is solved thanks to this forum post where JeremyF and Paenny explain how to get an on-screen keyboard.

The summary:

  • type: sudo apt-get install matchbox-keyboard
  • type ‘y’ say yes to installing
  • create a new file on the Desktop called ‘keyboard.sh’:
    #!/bin/bash
    matchbox-keyboard
  • Then open a terminal and write:
    cd Desktop
    chmod +x keyboard.sh
  • Now you can double click that file and execute it to start a big on-screen keyboard 🙂

PS: If you start the keyboard from a terminal, and then use that keyboard to type ctrl-c in that terminal your Raspberry will go nuts.