Rexplorer v1 – A robot programmed in Scratch GPIO

Rexplorer v1 – A robot programmed in Scratch GPIO

For the first time, I’ve built a fully functional robot with my trusty Raspberry Pi 2. My last attempt at connecting a motor driver to a Pi resulted in a burnt pi; but that was because I connected its 5v output to the driver’s 5v output pin (recipe for disaster).

Rexplorer is the successful result of research preceded by a burnt Raspberry Pi Mod B and an L298N motor driver which blew a fuse.

By the end of this tutorial you will have a fully functional obstacle detecting robot (provided you follow the instructions carefully).

Disclaimer: The author of this tutorial will not be held responsible for any burnt Raspberry Pi(e)s or indirect damage caused to the same due to negligence (as was the case with the author’s).

As you may already know, the Raspberry Pi is a Linux OS computer.

So first, lets take a quick look at the bot (when unplugged from the monitor/screen, keyboard, mouse and any other peripherals).

HARDWARE

  • Raspberry Pi – Model B 2 or any 40-pin Pi; you can also use Model B, A+, B+ or Zero if you know their respective pinouts
  • Raspberry Pie – Optional, unless you’re hungry
  • Motor Driver – Preferably based on L298N
  • BO motors – 2 nos.
  • Wheels for BO motors – 2 nos.
  • Castor wheel  – 1 BIG wheel
  • Mechanix/Mechano built chassis – You can use any chassis for provided there is space for motors and sensors
  • Jumper wires – As many as you can find
  • A power bank – Only to power the Raspberry Pi and can be substituted with any decent 5v power supply
  • Lithium ion 12v battery pack – To power up the motors.
  • Toggle switch – Optional, as it will be used for the power supply to the motor driver

The mouse, keyboard, monitor/screen and other peripherals which are connected to the Pi when it is not running headless (without a display), will now be referred to as the ‘station’.

The HDMI cable connecting the Pi’s display output should be at least one metre long.

The software of PiBot is done in a modified  version of the programming language (interface actually) called Scratch. The modified version which we will be using is called Scratch GPIO (version 7).

Scratch GPIO is modelled on Scratch v1.4, but in addition to all the other features, it also gives a user direct access to all of the Pi’s GPIO pins. This makes it easy to program the Pi as a robot with motors, sensors and various other hardware peripherals.

Connect all the hardware as shown in the schematic…

pibot_schematic

Or as explained here in words…

The motor driver’s 2 enable and 4 input pins are connected to pins 31, 32, 35, 36, 37, 38 of the Pi respectively; the motors are directly connected to the driver’s output terminals; the driver’s power supply is connected to the 12v battery (directly or with a toggle switch in between) and the GND/negative wires is also connected to the Pi’s GND (pin 40); the proximity sensor’s VCC is connected to the 3.3v output at pin 1 of the Pi, GND at pin 6, and O/P (output) at pin 7.

Between the two, I think you’ll prefer the schematic 😜

SOFTWARE

Boot the Pi to install Scratch GPIO with an internet connection.

Open the LXterminal and type:

wget http://bit.ly/1wxrqdp -O isgh7.sh

After the installer has been downloaded, type:

sudo bash isgh7.sh

Now that Scratch GPIO 7 and its accessory files are installed, let’s get coding!

Open Scratch GPIO7 (you’ll see a shortcut on the desktop) and drag the following blocks into the blank grey area:

pibot_code2

pibot_code3

The ‘broadcast’ blocks normally send messages which act as signals in the programme, but in this case they’re actually changing the state of the GPIO pins. The syntax for any output through broadcast is as follows:

pin<pin number><state>

Where ‘pin number’ is the GPIO pin number as per the pinout numbering and ‘state’ is the output at that pin i.e. either high or low.

Test it by clicking on the arrow keys on your Pi’s keyboard and make sure that the pin numbers are correct.

Right now, the robot should move forward, backward, turn left or right depending on the arrow key pressed. This is a quick and easy way to test your motor driver connections and make sure the wires connect the right pins.

To make it ‘smart’ we need to throw in another script for, let’s say, obstacle detection.

Open the background’s script area by double clicking on its icon near the list of sprites.

Now drag these blocks into the grey area:

pibot_code1

Save the project and close ScratchGPIO7.

Lastly, open an LXterminal window and type:

sudo raspi-config

A configuration list should open. Choose option 3 i.e. Enable boot to Desktop/Scratch and at the next list, choose Desktop log in as user ‘pi’ at the Graphical Desktop.

Now reboot your Raspberry Pi and watch the magic unfold!

As soon as the desktop loads, open ScratchGPIO7, open your project (if it isn’t already open) and hit ‘Enter’. The green flag script(s) should start automatically. The motors will now be activated so please check if the Pi is connected and accordingly hold on to it to prevent any untoward incidents.

The Pi can also be accessed wirelessly over SSH (Secure Shell) and VNC (Virtual Network Computing), so you could consider installing RealVNC on the Pi and a laptop/PC to control it (the robot) remotely.

The robot should (as every good obstacle detector does) stop when it detects an obstacle and continue moving otherwise.

Project GEIT

Project GEIT

During the last week of my Diwali vacations, I had the wonderful opportunity to be part of Project GEIT (Goans Empowered with Information Technology) and teach Scratch programming as a volunteer instructor to students at 3 village schools.

The project was divided into 10-day camps conducted at the following schools:

The aim of Project GEIT is to inculcate knowledge of computer programming and logical reasoning in village school students at an early age.

A huge advantage of using Scratch over many other development environments is:

  1. It has a very well-designed Graphical User Interface which makes it easy for even complete beginners to learn programming on their own.
  2. All the required components (if, else, print, delay, etc.) are present in the form of colour-coded blocks with specified shapes depending on what type of block it is (e.g. Input blocks are light blue, function blocks are yellow, animation blocks are deep blue, etc.) The blocks are also labelled in simple language which makes programming very simple. All the person has to do is drag & drop the blocks in the right places to make their program.
  3. And best of all, it works offline! At such schools, where there is no internet/broadband connection, Scratch was a blessing!

Ms. Sangeeta Naik (Project Lead) and Mr. Vincent Toscano (Backup Lead and Chorao School anchor) introduced me to project GEIT and asked me whether I would like to volunteer.

I jumped at the opportunity and a few days later, took my first session at Azmane High School (17th November). Following that, I went to St. Bartholomew’s High School (19th & 20th November) and Dayanand High School (21st November).

It was amazing to see so much enthusiasm in the students, many of whom share a passion for coding. The excited grins on their faces on accomplishing the challenges thrown to them, was priceless.

I thoroughly enjoyed the interactive sessions at all the schools and met some wonderful people. It was an incredible learning experience and one of my most well-spent vacations.

Thank you GEIT, for giving me the opportunity to be part of this revolution.

Special thanks to Sangeeta Naik, Vincent Toscano, Jessica D’Abreo, Mona Parras and Pradeep Gaonkar.

Press conference at Goa Chamber of Commerce & Industry at the end of the 10-day camp

Internet Of Things with Raspberry Pi – 2

Internet Of Things with Raspberry Pi – 2

This is a follow-up to my previous tutorial Internet of Things with Raspberry Pi – 1. (If you’re not familiar with web servers, PHP coding and the basic concepts of IOT, you should probably go through part 1 first.)

In the previous tutorial, we set up an Apache 2 web server on the Raspberry Pi and created a web page for it, from which we could control an LED.

In this tutorial, we’ll be adding controls for playing videos and songs on the Raspberry Pi using the OMX media player.

DSCN4341

The setup

Step 1:

Connect the following to the Raspberry Pi as shown in the pictures:

  • Power supply
  • HDMI (if your screen/monitor doesn’t have an HDMI port, you can use a VGA to HDMI converter)
  • Ethernet (to internet)
  • USB Keyboard and mouse (unless you’re accessing the Pi over SSH or VNC)

FII90WGIBNHIEO9.MEDIUM

Step 2:

Run the following command to install OMXplayer on the Raspberry Pi:

sudo apt-get install omxplayer

You’ll be using this player to play media on your Pi.

Step 3:

Start by downloading two to three nice songs and shorten their file names to something small. (e.g. ‘Where the river flows by Scorpions’ can be shortened to just ‘WhereTheRiverFlows’) Long names also work, but they take too much time to type when coding. Now put all the songs into a folder called ‘songs’. Make this folder in ‘/home/pi’.

Step 4:

Similarly, create a folder in ‘/home/pi’ called ‘videos’ and add a video in it. Follow the same steps as earlier for naming it.

Step 5:

Run the following commands to open your current web page code:

sudo nano /var/www/index.php

Add the new parts as shown below or just copy this code to the index.php file:

<?php

    if (isset($_POST['on']))
    {
		exec("sudo killall python");
		exec("sudo python /var/www/mystuff/ledON.py");
    }
    else if (isset($_POST['off']))
    {
		exec("sudo killall python");
		exec("sudo python /var/www/mystuff/ledOFF.py");
    }
    else if (isset($_POST['blink']))
    {
                exec("sudo python /var/www/mystuff/ledBLINK.py");
    }

//New code starts here
    else if (isset($_POST['WhereTheRiverFlows']))//Your first song
    {
                exec("sudo omxplayer /home/pi/songs/'Where The River Flows.mp3'");
    }
    else if (isset($_POST['StarWars']))//Your second song
    {
                exec("sudo omxplayer /home/pi/songs/'Star Wars.mp3'");
    }
    else if (isset($_POST['BezubaanPhirSe'])//Your third song<br>
    {
                exec("sudo omxplayer /home/pi/songs/'Bezubaan Phir Se.mp3'");
    }
    else if (isset($_POST['bell']))//A funny sound clip
    {
                exec("sudo omxplayer /home/pi/songs/'bell.mp3'");
    }
    else if (isset($_POST['Presentation']))//Your video
    {
                exec("sudo omxplayer /home/pi/videos/Presentation.mp4");
    }
    else if (isset($_POST['stop all']))//This is to stop all media
    {
                exec("sudo killall omxplayer.bin");
    }//This command stops all media
?>
<html>
	<style type="text/css">
//Button colour is now yellow and size has been changed
		#form{font: bold 30px/30px Georgia, serif;}
		button{background: rgba(255, 255, 0, 0.99); width: 250px; height: 180px;border: none;border: 3px solid black;border-radius:20px;}
		#container{margin0px; auto;width:80%;min-width:40%;}
	</style>
	<body>
	<div id="container">
		<form id="form" method="post">
			<center>
			<button name="bell"><h1>Bell</h1></button>
<br><br>		<button name="on"><h1>Led ON</h1></button>
			<button name="off"><h1>Led OFF</h1></button>
			<button name="blink"><h1>Led BLINK</h1></button>
	//This part is contains the new buttons
<br><br>		<button name="StarWars"><h1>Star Wars</h1></button>//Your first song button
			<button name="BezubaanPhirSe"><h1>Bezubaan Phir Se</h1></button>//Your second song button
			<button name="WhereTheRiverFlows"><h2>Where The River Flows</h2></button>//Your third song button
<br><br>		<button name="Presentation"><h1>Presentation</h1></button>//Your video button
<br><br>                <button name="stop all"><h1>Stop all media</h1></button>//The button to stop all media
			</center>
		</form>
		</div>
	</body>
</html>

Step 6:

Connect to your local network from a smartphone or laptop and try playing some tunes! You’ll notice that the control panel now looks a lot more cheery with some colour and font size editing.

DSCN4342
Modifying the code to your personal requirements is relatively simple once you get the hang of it. So go ahead. Add some more songs and videos.
I also added a small code for a bell sound clip so that whenever someone is at my front door, they can connect to the open wifi network and ring the ‘IOT bell’ instead of the door bell!

Internet Of Things with Raspberry Pi – 1

Internet Of Things with Raspberry Pi – 1

When I was new to IOT (Internet Of Things), I saw that there were hardly any tutorials which were simple enough for a beginner to understand and try out. There was either to much technical jargon, or the hardware was too complex.

So now that I’ve played around with IOT a bit, I decided to make a 10 step tutorial on controlling an LED over a Local Area Network (LAN).

In this tutorial, we’ll be using an LED, a Raspberry Pi, a Wireless ADSL Router with internet connection and a device with a web browser. (Smartphone, Laptop, Computer, PSP, etc.)

On the software side, we’ll be using Apache2MySQL and PHP.

If you’re new to the Raspberry Pi, you might want to have a look at Getting started with Raspberry Pi before trying out this project.

(Note: This project only uses an internet connection for software installation. After the installation and coding is done, the internet connection is not required. For more info on making the project available on the internet, check port forwarding)

Components:

  • Raspberry Pi (I’ve used a Raspberry Pi 2 model B, but any model will suffice)
  • ADSL Wireless Router
  • Power adaptor for the router
  • Computer monitor / TV screen which has an HDMI/VGA port (If you’re using a VGA port then you will have to use a VGA-HDMI converter)
  • Ethernet/LAN cable
  • 2 Female-Female jumper wires
  • Small LED
  • USB Keyboard and Mouse
  • A computer/laptop connected to the same modem as the Raspberry Pi (This will just be for the final test so even a smartphone is ok)

Hardware Setup:

DSCN4191    DSCN4186

DSCN4188    DSCN4198

Step 1:

Start your Raspberry Pi and open the Graphical User Interface (GUI) with the command:

startx

Step 2:

Once the interface is active, open the terminal and type the following commands:

sudo apt-get install apache2 -y

An IOT webpage will require a web server. This command will install a web server called Apache2.

Step 3:

To test the web server, you will need to know your Raspberry Pi’s IP address. Enter the command:

hostname -I

A number will be displayed. Start your Pi’s web browser and enter this number in the search engine.

You should see something like this:

apache-it-works

Congratulations! Your Apache server is up and running!

Step 4:

This is a default webpage which is stored in the ‘/var/www’ directory. To make changes to it and customise it, you need to enter this command:

sudo nano /var/www/index.html

Whenever you’re modifying a file, don’t forget to add ‘sudo’ at the beginning. This indicates that you are modifying it as a superuser.

Press Ctrl + X and hit enter to exit the file.

Step 5:

You will also need a preprocessor called PHP. Install it with the following command:

sudo apt-get install php5 libapache2-mod-php5 -y

Step 6:

Now enter the following commands:

cd /var/www

sudo rm index.html

sudo nano index.php

The last command will open a blank file. You will need to enter some PHP content to test it. Type the following:

<?php echo “hello world”;

Open the web browser and refresh the same web page. It should display ‘hello world’ in the top right corner.

You could also try:

<?php echo date(‘Y-m-d H:i:s’);

This will display the current date and time.

Step 7:

Install MySQL with the following command:

sudo aptget install mysqlserver php5mysql y

When installing MySQL, you will be asked for a password. You will need to remember this password in case it is required in the future.

Step 8:

Enter the following commands:

cd /var/www

sudo rm index.php

sudo nano index.php

The last command will open a new index.php file. Enter the following text in it:

<?php

    if (isset($_POST[‘on’]))

    {

exec(“sudo killall python”);

exec(“sudo python /var/www/mystuff/ledON.py”);

    }

    else if (isset($_POST[‘off’]))

    {

exec(“sudo killall python”);

exec(“sudo python /var/www/mystuff/ledOFF.py”);

    }

    else if (isset($_POST[‘blink’]))

    {

                exec(“sudo python /var/www/mystuff/ledBLINK.py”);

    }

?>

<html>

<style type=”text/css”>

#form{font: bold 12px/30px Georgia, serif;}

button{width: 150px;height: 75px;border: none;border: 3px solid black;border-radius:20px;}

#container{margin:0px auto;width:80%;min-width:400px;}

</style>

<body>

<div id=”container”>

<form id=”form” method=”post”>

<center>

<button name=”on”>Led ON</button>

<button name=”off”>Led OFF</button>

<button name=”blink”>Led BLINK</button>

</center>

</form>

</div>

</body>

</html>

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

Step 9:

You will now need the Python files for controlling the LED.

There are three Python files. One to turn on the LED, one to turn it off, and one to make it blink.

Please note that the following Python codes are for Raspberry Pi models with 40 pins.

i.e. Pi model A+, Pi model B+ and Pi 2 model B

If you’re using a 26 pin Raspberry Pi (Model A or B), then you will have to change the GPIO pin number in all three codes to 13 instead of 40 and accordingly connect the LED.

Use the jumper wires to connect the negative lead of the LED to Pin 6 on the Raspberry Pi’s GPIOs and connect the positive lead to Pin 40. (Pin 13 in the case of a 26 pin GPIO Raspberry Pi.)

First, let’s create a file to turn on the LED. Enter these commands:

cd /var/www

sudo nano ledON.py

Type the following text in the blank file:

import time, RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(40,GPIO.OUT)
GPIO.setwarnings(False)
GPIO.output(40, True)
time.sleep(1)

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

Now create a file to turn it off:

sudo nano ledOFF.py

Type the following text in the blank file:

import time, RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(40,GPIO.OUT)
GPIO.setwarnings(False)
GPIO.output(40, False)
time.sleep(1)

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

Lastly, create a file to make it blink:

sudo nano ledBLINK.py

Type the following text in the blank file:

import time, RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(40,GPIO.OUT)
GPIO.setwarnings(False)
while True:
GPIO.output(40, False)
time.sleep(1)
GPIO.output(40, True)
time.sleep(1)

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

 Step 10:

Now, you will need to change certain file permissions. Enter the command:

sudo nano /etc/sudoers

This will open a file which contains permissions for directories, files, etc.

Go to the last line of the file which says:

pi ALL=(ALL) NOPASSWD: ALL

Below it, type this:

www-data ALL=(ALL) NOPASSWD: ALL

Exit the file by pressing CTRL + X. You will be asked if you want to save changes. Press Y and hit enter.

Reboot the Raspberry Pi with the command:

sudo reboot

Test the setup!

Congratulations! Your first IOT project is now ready! You can try it out from any device which is connected to the same network as the Raspberry Pi.

If you’re having problems with this project, go over the whole tutorial once again. If it still doesn’t work, then feel free to contact me on sataklela@gmail.com

Once you know for sure that everything is functioning properly, try modifying the codes to play songs, run motors, etc.

You can even add a small relay circuit and control the lights in your house!

IOT is an amazing thing and once you understand it, there are almost no limits to what you can do.

In the video below, I tried to control the LED using my PSP (Play Station Portable):

DSCN4196

The final setup with minimal connections

For more, check out Internet Of Things with Raspberry Pi – 2.

Summer Internship 2015 – Inventrom

Summer Internship 2015 – Inventrom

Once again, this summer, I interned for a month at Inventrom Robotics, an electronic technology start-up based in Goa with primary focus on Robotics and Wireless devices.

IMG_20150529_182303464_HDR

I joined at a time when their IOT (Internet Of Things) product ‘Netplug‘ was in the R&D phase. Netplug is a single unit which can be connected to any physical device to control it over the internet.

My task was to integrate it with RGB LED lighting (multicoloured lights).

This involved writing Python scripts, on a Raspberry Pi, to automatically run through a cycle of 6 colours at various speeds. Depending on the area of usage, this could be used as mood lighting in bedrooms & lounges, or to reflect the energetic atmosphere of party zones, etc.

While I worked on the lighting system, my colleague Haston was simultaneously creating a backend program and a webpage for Netplug. Invariably, there would be conflicts between systems and processes. In such a case, we had to alert each other before rebooting the system or stopping backend programs.

Other quick jobs also included:

  • Packing kits for workshops
  • Assisting during workshops
  • Testing the ‘Chicken Smasher‘ app for bugs

believe it is essential for everyone to take up at least a part-time summer job to hone one’s skills on one’s subject of interest. This lets you spend your vacation time creatively, while helping you understand non-technical aspects of running a company like, time management, ensuring deadlines are met, keeping databases of contacts for future use, developing people skills, etc.

Working at Inventrom was truly a rich, educational experience which I will never forget.

Left

Left to Right: Pranav Pai Vernekar, Raunak Hede, Ryan D’Costa, Pranav Kundaikar, Haston Silva

Mouse controlled Sign Board using Raspberry Pi & Topping Pi

Mouse controlled Sign Board using Raspberry Pi & Topping Pi

This is a mouse controlled signboard made using Raspberry Pi (the world’s smallest computer) and a new circuit board called Topping Pi (designed and manufactured by Inventrom Robotics, Goa). The program was written in Scratch GPIO 5. A simple programming language, yet amazing in capabilities! Click on the picture to see a video of the awesome signboard and an even more awesome ME!