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.

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 (or downoad from here). Then add execute permission on that file with the command chmod 755 Then you can run the file with ./

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

New robot project!

Last saturday I went to an auction of random old electronics. After some hours of boring junk and slightly interesting items being sold for way too high prices, there appeared what seemed to be a big robot on the stage. So my friends and I started a quick discussion on how much we were willing to pitch in together as a maximum bid, because we just could not let this beauty slip past us. You might be able to imagine that we were a bit disapointed when it turned out to be not a robot, but some weird video conferencing system. When the bidding opened I shouted the minimum opening bid of 1 Swedish crown from the back of the audience. Expecting the rest of the room to take over the bidding from there, I could feel my heartrate go up while a silence was all that came.

Wait, what just happened? I just bought this enormous yet awesome-looking thing for 1 kr? And how will I get it home? (I came there by foot, bringing a backpack for anything I would buy) And, what just happened? In the end I went to get my car and brought it home. (It filled the whole back side of the car)

So now I am the happy owner of a Tandberg Maestro. It seems to be in a near perfect condition, except for some dust and one broken power cable (easily replacable). The only problem for any intended use though is that it is incomplete. The biggest miss is the remote control, which according to the manual I found online, is rather crucial. As for the cables I’m not sure yet if I am missing anything more than a power cable, because the manual is not very clear on how everything should be connected.

A quick overview of the system:
– On top a good quality Sony camera with optical zoom and steppermotors for pan and yaw.
– Below that a 5(?) inch color display
– Next a set of speakers, which seem to be optimised for speech instead of music
– On the back side hidden behind an easily removable cover is the ‘codec’; the big black computer with a whole bunch of ports
– This is all mounted on a strong metal pole/frame that is attached to the base part of the frame which has a lot of empty space and some plastic wheels.

As far as I could test, all systems seem to work. I can get the video out of the camera from the s-video port. It also has a composite video port, but this seems to only give a black and white video. The steppermotors work, because they calibrate when they get power. The optical zoom can be tested by two buttons on the circuitboard of the camera. The speakers work directly when they get power. I have not figured out yet what the ‘out loop’ port is for, but I assume some kind of output of the same sound again? The screen was a little bit trickier to test. In the end it turned out that if I connect its data cable to the data1 port on the (powered) codec, the screen accepts input on the s-video port. I dug up my old laptop, which to my surprise has a s-video port, so I managed to connect the 5 inch screen as external screen. This also shows that the codec is not broken. But without the remote I will have a hard time testing anything more with it.

I have not completely decided yet what my plan is. At first I thought of building in a raspberry pi as the brains for the robot, but now that I found my old laptop with s-video port it could be an easier path to use that instead. I would like to get rid of the codec box, but then I need to find a way to fool the screen so that it works without it. Also I need to figure out how to control the zoom and movement of the camera via the ethernet cable that plugs into it. And I have not found yet how I will take the video feed as input into any computer system. Maybe a s-video to usb cable would fix that.

Some ideas:
– Motorized wheels! If this is to be a robot, it should be able to move itself around. The ‘desk chair kind of wheels’ on the back are a bit small and crappy, so those should get an update anyway. But motorized wheels might be best on the front?
– Some kind of computer as brain to control the different parts of the robot. Currently it is most likely to be old laptop.
– Wireless powersource, aka battery. The camera and speakers both like to get 12 volt, so maybe the battery should give that?
– Web based control
– Voice synthesis, or maybe an R2-D2 style of beeps. (Or both)
– A robot arm. The bar on the back is just to perfect to mount stuff on

What about a name? Maestro sounds quite nice, and is already printed in big letters on the front. (I can peel off the ‘Tandberg’ letters) Or would some number/letter combination be more nerdy? And what kind of function should this robot fullfill?

Stay tuned for more 🙂

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
  • 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 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@ 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!

Print your own tools

Now that I am in the process of designing and copying things as 3D models I notice I really need to have a good ruler to figure out the sizes. Specially a caliper (Dutch: schuifmaat, Swedish: skjutmått) would be good. I know where to borrow one, but I should just get one for myself. This morning I asked myself, “Why would I buy one, if I could also print one?”. So instead of looking up the opening hours of the store I opened Tinkercad. A quick search only gave me parts of the tool, but they were good as inspiration.

Iteration 1
As usual it went surprisingly quick to create the model. Of course it helps that the design is simple and consists of basic geometric shapes. This close-up picture was also helpful for the details. When I was done I had modeled the entire tool (Link). But I wasn’t sure if it would fit together, look good, have the right scale etc. So then I took the model, cut out only the first part, and printed that out. In this way I could test the model without spending a lot of time and material.

Caliper test print 1
Caliper test print 1

Iteration 2
It turned out it was a good idea to do a test print. The scale of the ruler was correct, and the model looked quite good. A big issue though was that because of the big overhang there was a lot of support printed, which was hard to take away. In my second version I changed this to a small overhang, with an angle of 45 degrees (so it is supported by the lower layers and thus does not need support). I also added some room so the two pieces would be able to fit into each other. In my original model the inner part was exactly the same size as the empty space, which in real life would not work.

Caliper test print 2
Caliper test print 2

Iteration 3
The second printed turned out really well. The parts fitted together, and the tool seemed to work. Still I made some changes again. The extra space was reduced to 0.25mm on both sides, as the 0.5mm was more than what was needed. Also the height of the stripes of the scale was brought down to 1mm to make sure they would not break off. And I added numbers to the ruler, otherwise it would be kind of annoying to use. This time the model became a full size caliper again because I had faith that it would work.

Print 3 warped
Print 3 warped

Unfortunately now that I was printing the entire tool it took the printer too long to print one layer, which made the plastic cool down and curl up. I hoped that later layers would push it down and correct it, but no. This of course caused the parts to not fit together, so I was forced to cut it shorter.

Print 3 cut shorter
Print 3 cut shorter

In the end, the part that did come out straight fits very well, so I am rather pleased. It works as a tool to measure objects in millimeters. I think my next move will be to print this same model again using the Replicator 2x because it is enclosed and has a heated building plate, so it should not warp as much. I will also print the two parts separately to reduce the time it takes to print one layer. If I print it from ABS it will probably also warp less than the PLA I used today. (One idea which I am not going to test would be to try a faster printing speed.)

Link to the final model.

Lessons for today:

  • It is a good idea to try a small test print of the difficult parts of a big model.
  • The measurements in the software transfer correctly to the printer. (With this I mean the scale of the ruler was correct.)
  • 0.25mm on both sides of an object should be a good size to make it fit quite precise inside something else.
  • Reducing overhang can be very rewarding.
  • Try to prevent warping.

Is it broken? 3D print a new!

Our beloved but always overloaded laundry bag is held together by a string with a ‘clip’ (no idea what else to call it) on it. At least that is how it used to be, until it broke. Since I was going a bit nuts already with drawing things in Tinkercad, it was a perfect practice object.

The original broken rope clip
The original broken rope clip
The parts
The parts
The 3D model
The 3D model
Printed parts after removing the support material
Printed parts after removing the support material
They fit together
They fit together
It works!
It works!

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 ‘’:
  • Then open a terminal and write:
    cd Desktop
    chmod +x
  • 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.

3D printed Leatherman wave extension v0.1

Last month I became the proud owner of a Leatherman Wave. Because the lanyard ring it has is too well hidden, I ended up on Youtube to figure out how to open it (The answer). There I also came across some cool mods for Leatherman tools, which inspired me to think beyond the tool as how it comes by default.

Quick-Release Lanyard Ring & Removable Pocket Clip
Quick-Release Lanyard Ring & Removable Pocket Clip

Two accessories that are available for the Wave (and other models) are the quick-release lanyard ring and removable pocket clip. Now I tried to re-create this as 3D model and print one myself with a 3D printer. The model is publicly available on Tinkercad.

In the first version the measurements were a bit guessed, so after printing some filing was needed to make it fit. Here are some pictures of the result:

Result straight after printing
Result straight after printing
After filing
After filing
It fits
It fits

It fits

The next step will be to update the model with better measurements, and improve its strength. Because so far it seems a bit too thin to hold much force. The idea is to later invent other tools with this form on it so they can be attached to the Leatherman.

To be continued…

Steer a Double Robotics robot with your Android’s accelerometer

Double Robotics hack from Niels Swinkels on Vimeo.

The office where I work recently bought a Double Robotics telepresence robot. Imagine an iPad ontop of a Segway, and you will have a pretty good idea of how it looks and behaves. The fun thing of course is that you can start a video call to it. Your face will show up on the iPad, and you can see what the robot sees and tell it where to go.

One thing that disappointed me a bit though was that only iPhones, iPads, or computers with the Chrome browser could be used to connect to the robot. I mean, why can I not do it from the Chrome browser on my Android phone? After a short dive into the source code of the Double Robotics webpage I figured out a way to make it possible, with a little bonus functionality in the end.

Let’s dive in

Everything happens on the page First you log in. Then you connect to your robot, and start the video call. The interface changes, you see what the robot sees, and you can steer it. After the logging in most is done with the help of Javascript. This is quite easy to figure out by reading the source code of the webpage. On line 31 for example you see a reference to a Javascript file called drive.js. There you can find all the functions used to control the robot. The fun thing with Javascript is that we, as the visitor of the webpage, can use it as well.

Make a connection

If you have not done so already, go to and log in with your account. Right-click somewhere on the page, and select View page source. If you do not see it in the menu, try right-clicking on a piece of text on the page. Or via the menu: Menu > Tools > View source (Ctrl+U) (Chrome).
Now search (Ctrl+F) for connectTo. You will find something like this:
Copy this to somewhere (notepad) so you can use it later. This function is called to start the video call with your robot. I assume that the number is some form of serial number. If you now type the following in your address bar, you should see the connection starting (Use the serial number you just found):

One small step..

As I wrote before, in the file drive.js you can find the functions used to drive the robot. Driving and steering is done with one function:
sendCommandWithData(8, { "drive" : -100, "turn" : 0 });
Here the drive number means how far you go forward (negative numbers) or backwards (positive numbers), and the turn means rotate right (negative numbers) or left (positive numbers). The default speed for going backwards is 50, and for making a turn is 100 or -100. Feel free to experiment with other numbers, but make sure you catch your robot if it falls! It seems that the numbers represent the amount of movement, and not the speed with which you move.

What more can we do?

Here are some more functions you can call. This is not a complete list. If you want to find all possible actions, try searching in the Javascript yourself.

sendCommandWithData(9, { "pole" : 200 });
Where 200 means move the pole up, -200 means down. Smaller numbers mean less movement. In this way you can position the pole at any height you want, not just all the way up or down. The only weird thing is that the robot might think that the pole has moved all the way up or down, and refuses to move more. To go around this you just move it once in the opposite direction, and then quickly a few times in the direction you want it to go.
Switch camera

I’m lazy.. less typing please?

So all of these functions you can call by typing javascript: followed by your list of commands in the address bar. I admit, that is not the easiest way to drive a robot. A better way is to create bookmarks. (In Chrome:) Click menu > Bookmarks > and put a checkmark in front of Show bookmarks bar. Then create a new bookmark by right-clicking the bookmarks bar and selecting Add page.... As name write Forward and as url javascript:sendCommandWithData(8, { "drive" : -100, "turn" : 0 });. Now you can click your Forward bookmark to make the robot drive one step forward. In the same way you can create more buttons for the other functions.

forward bookmark

Go mobile

If you felt adventurous and tried this in the Chrome browser of an Android phone, you will have noticed that opening a bookmark via the menu does not work in this case. That is because as soon as you open the bookmark menu, you leave the Double Robotics webpage. The way around that problem is to start typing the name of your bookmark in the address bar, and click on it when you see it as a suggestion.
forward bookmark mobile

Sync bookmarks

For those of you who have set up all the bookmarks in Chrome on their computer, and now want to have them on their mobile Chrome as well, there is good news. Our ‘good old friend’ Google has built in a synchronization functionality, which copies your bookmarks between devices. To turn it on do the following:

  1. In Chrome on the computer, go to Menu > Sign in to Chrome... and sign in
  2. Then go to Menu > Settings and click on Advanced sync settings...
  3. There you can select what should be synced between devices. In our case we only need bookmarks.
  4. On your Android phone, open Android settings, and find Google under the heading Accounts
  5. Click on your account, and select Chrome
  6. Under the menu (The three dots in the upper right corner) select sync now
  7. You should now find your bookmarks in your mobile Chrome in a folder called Desktop bookmarks

Droids in control

And at this point we can now connect to our robot, from an Android phone, even though Double Robotics tells us we can not. Just log in, and use the connectTo function.

More customization!

Still this whole business of bookmarks is not really that awesome, specially not on a small touchscreen. Good thing that Javascript has some more tricks up its sleeve. What if I told you we can add our own interface into the webpage? Add this next code as one bookmark, and use it after having started a connection to your robot:

javascript:var interface = document.getElementById('sessionButtonsColumn');var btnForward = document.createElement('button');btnForward.setAttribute('onclick', 'sendCommandWithData(8, { "drive" : -100, "turn" : 0 });');btnForward.innerHTML='forward';interface.appendChild(btnForward);var btnBackward = document.createElement('button');btnBackward.setAttribute('onclick', 'sendCommandWithData(8, { "drive" : 50, "turn" : 0 });');btnBackward.innerHTML='backward';interface.appendChild(btnBackward);var btnLeft = document.createElement('button');btnLeft.setAttribute('onclick', 'sendCommandWithData(8, { "drive" : 0, "turn" : 100 });');btnLeft.innerHTML='left';interface.appendChild(btnLeft);var btnRight = document.createElement('button');btnRight.setAttribute('onclick', 'sendCommandWithData(8, { "drive" : 0, "turn" : -100 });');btnRight.innerHTML='right';interface.appendChild(btnRight);

This will add four buttons to your interface which you can click to drive the robot forward, backward, left or right. It works by finding the part of the interface which contains the already existing buttons
and appending four new buttons to it.

With a little bit of Html knowledge and some experimentation you can change this code to create any shape of interface you want.

Are we done?

Nope. Because one of the latest cool things on the web has been Html 5. And one of the things that makes it cool, is that it gives you access to the accelerometer of your smartphone. Html 5 is for a big part Javascript, so it is a piece of cake to add this coolness to our robot script.

Based on this very clean example of how to read the sensor values (Thanks Piotr and Oskar!), I made this code:

if (window.DeviceMotionEvent) {
window.addEventListener('devicemotion', function(ev) {
var acc = ev.accelerationIncludingGravity;
dmHdlr(acc.x, acc.y, acc.z);
}, false);
else {
alert("devicemotion not supported on your device or browser.");

var lastDM = new Date().getTime();
function dmHdlr(aX, aY, aZ) {
var currDM = new Date().getTime();
if (currDM < lastDM + 500) {return;} lastDM = currDM; var drive = 0; var turn = 0; if(aX < -3 || aX > 3)
turn = aX*10;
if(aY < -3 || aY > 3)
drive = aZ * -10;
sendCommandWithData(8, { "drive" : drive, "turn" : turn });

Add this, as one long line, as bookmark to your browser. Then connect to your robot, and select the bookmark. Now you can steer your robot by just tilting your phone! Hold it upright to stand still. Tilt the top away from you to go forward, or towards you to go backwards. Tilt the screen to the left to turn left, and to the right to turn right. Enjoy! Note: Please drive carefully with your robot, and do not forget you have quite a delay in response time.