Raspberry Pi 4×4 Truck

Raspberry Pi 4×4 Truck

Over the past weeks Michael and Alexander worked on 4×4 Truck which is controlled by a raspberry pi, this is what we came up with.

Even though this project is still work in progress, I would like to show you some snippets of the project.


Before we started this project we searched for extension boards for the pi to control motors over the gpio, but none of them could 100 percent fit our needs. Due this reason we planned the board in eagle cad.

This is our schematic:

Schematic PiTruck

We got a hint, that seeedstudio is manufacturing PCB’s at a very low price. The process is really easy, you have to create an account, upload your gerber files. If all your files are okay, they will start to manufacture. As we wanted to keep it cheap, we have chosen the lowest shipping rate. Thats why they took almost 3 weeks to ship.



To detect the motion of the smartphone we used the following script:

if (window.DeviceOrientationEvent) {
console.log("DeviceOrientation works!");
window.addEventListener('deviceorientation', function(eventData) {
var LR = eventData.gamma;
var FB = eventData.beta;
var DIR = eventData.alpha;

deviceOrientationHandler(LR, FB, DIR);
}, false);
} else {
alert("Not supported on your device or browser. Sorry.");

function deviceOrientationHandler(LR, FB, DIR) {
//for webkit browser
document.getElementById("imgLogo").style.webkitTransform = "rotate("+ LR +"deg) rotate3d(1,0,0, "+ (FB*-1)+"deg)";

//for HTML5 standard-compliance
document.getElementById("imgLogo").style.transform = "rotate("+ LR +"deg) rotate3d(1,0,0, "+ (FB*-1)+"deg)";

if(LR < -15)

  document.getElementById("dir").innerHTML = "Left" ;
url: 'Left.php',


else if(LR>15)

 document.getElementById("dir").innerHTML = "Right";
url: 'Right.php',



  document.getElementById("dir").innerHTML = "";
url: 'No_Movement_Servo.php',


if (FB > 40)

  document.getElementById("foo").innerHTML = "Backward";
url: 'backward.php',

else if (FB < -5)

url: 'forward.php',

document.getElementById("foo").innerHTML = "Forward";


url: 'No_Movement_Motor.php',

document.getElementById("foo").innerHTML = "";



For your understanding:

In this script we are checking if the device supports orientation if it does, we will show an image which will move in the same direction as the phone. Further we will call the PHP scripts with an ajax function if the movement (is greater than 5 or 15).

The ajax function looks the following:

url: 'Name_of_your_php.php',
//Change this to your needs

The php files which will be called look the following:

       //if we assume your using wiring pi, the prompt would look like this:
       shell_exec('gpio write 6 1');
       //if you want to run a python script, try this:
       shell_exec('python your_python_script.py');

If you are running script which need sudo rights you have to add the web server to the sudoer file. It might be also necessary to change the permissions for the www-directory

The servos are controlled by a small python script like this (simplified version):

#!/usr/bin/env python
# Servo Control
import time
def set(property, value):
                f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
                print("Error writing to: " + property + " value: " + value)

def setServo(angle):
        set("servo", str(angle))

set("delayed", "0")
set("mode", "servo")
set("servo_max", "180")
set("active", "1")

delay_period = 0.01



We will keep you up to date with news on this project!

Cheers Alexander 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.