Insect Robot

(UPDATED 5.4.2013)

This is my insect robot project. I am actually writing this as I am building my robot from the spare parts since its always best to document while doing something so you can have the accurate information from memory that is fresh. When I started this project I was (and still am) incredibly broke like every other student so I was not sure how I was going to make/start my project let alone finish but I got the spare parts for this project from our course mentor Tero Karvinen.

The parts that I am going to use for this project are

The Arduino Uno MicroController
Ultrasound sensor (HC-SR04)
– Two servos (both of them are HC-311 models)
– iPhone 5 box (R.I.P. Steve Jobs, sorry for making fun of your phones)
– Jumper wires
– Roll of metal
– BreadBoard
– Ducktape (lol)
– Glue

The aim of the project is to create and program an insect robot that would walk and stop if and object comes in front of it and if I have time to program it to change direction if object is in front of it. The hardest part of this project was and still is that I don’t have sufficient amounts of money to buy decent hardware, tools or anything else for that matter to make anything smart even though I am staunch enemy of making excuses but in this example this is the unfortunate reality but I did manage to get a good project all in all.

What I did was that I put the Arduino microcontroller along side with all the necessary hardware inside the iPhone box and I programmed the Arduino to give signals to the servos so that the servos would move the metal pieces that I glued to them, so the whole thing would look like it was walking. The problem I encountered in this project was that how to program this thing so it would move forward.

The problem arises with the servos. How am I to program this thing so it would walk forward when the legs are made of static metal/copper/tin bits not to mention to move direction. Because the project is low budget, the legs are made of solid metal, meaning that this Frankenstein monster does not have knees to lift up, so I had to program this thing to somehow push itself forward. I did found some tips from the book “Make: Arduino and bot gadgets” but I thought to make this thing look more like it was built by me so I created something that is not pretty to watch but it works (on some level).  Below is the picture I took from the side of the robot


As you can see in the picture, that I have put some tape under the insects metal legs so it would be able to push itself forward. Without the tape this thing would slip all over the floor and not go anywhere because the metal would not be able to push on solid wood floor. I also was not able to have batteries on board this insect because the legs are very fragile and the legs would just collapse under the pressure. The best method would have been to buy some solid tin or copper tubes but then again this was supposed to be a personal project so the solution was not bad. I only regret that I did not buy a soldering gun  to solder the legs to the servos. Anyway the functionality of this robot is just to walk and stop for 5 seconds if something comes in front of it (more than 20 cm closer) and continue to walk forward if the object has moved that was in front of it. Yes I know it is a bit silly, because if a wall is in front of it, it would just stand in front of it. Below is another picture from the front side of it.


You can see from the pictures that I duck taped the breadboard on top the the box and attached the ultrasound sensor on top of it. If you want to have more info about both the functionality of the servos and sensors please look for my previous assignments or go to the top of this post for the datasheets. Below is the code

#include <Servo.h> // Library
#define trigPin 8

#define echoPin 9
Servo servo1;
Servo servo2; // The making of servo variable
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo1.attach(11); // Attach variable to pin 11

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance <= 10){
}else {
servo1.write(20); // move the servo with distance
servo2.write(120); // move the servo with distance

Unfortunately as I am writing this and going forward with my project, the legs of the robot have collapsed. I just glued it together so I am not sure how it will turn out later. Below is the video from my previous advancement few hours ago before I broke the robot.

I promise to upload the new video if I get my robot to walk again. By the way, something that I forgot to add in the beginning of my course posts was the link to the existing other peer links in my same class. Here is the link to my course mentors site

Scroll down to see other people with their own projects in this course. I also did not manage to get the insect up and running since the glue simply is not enough for the legs like that, because I did not attach the legs properly around the servo wings. I will try to make a better project later with more ambitious plans with better equipment.


Make: Bots and Gadgets by Tero Karvinen and Kimmo Karvinen

BUS4TN007-3 / tehtävä 4 (assignment 4)

In this weeks assignment I had HC-SR04 Ultrasonic sensor that measures distance (Pictures below). I am sorry for the quality of the photos but it is something that we have to live with since I try to use as much as my own material as possible instead of using outside sources because a lot of the background information that I use in my assignments are from online sources and the coursebook that I have mentioned in the sources.

Anyway lets start with the assignment. Again I encourage you to see my older assignments for some background information. So when I started this assignment the very first problem that I encountered again was not being able to find the datasheet for this particular sensor but in the end I did manage to find the datasheet. Click this link to read the datasheet.


Note in the above picture that there is 4 pins and above the pins there is descriptions that mean the following:

VCC  = 5 volt supply
trig    = Trigger pulse input
echo = Echo pulse output
GND = Ground




So this sensor is all about using ultrasound to measure the distance of the object. The mechanism is close to how bats locate their food, other bats and objects. Basically it sends signal and the input comes when it bounces back from nearby objects. This way the sensor is able to calculate how far the object is. Please do note that it’s not easy to explain how sound moves from someone like me but I do know (Thanks to the internet) some basic things about it.The speed of sound = 343 seconds per meter or c = 343 m/s depending on the temperature of the air among other things. The sensor thus sends ultrasound (The speed depends) “where the measuring angle is 15 degrees”. Other things you should note are

– You should not worry about any health effects for small projects like this

– The sensor sends pulses (Look below and click on it to go to the original source)


Before I start with the assignment I must admit that I heavily borrowed code from other people because the test code looks pure evil but here is the link. I think that you can calculate the distance of the object by taking humidity into account also, but that is a bit more complicated code and I do not have time to go into that area.

The example code that I will be using is not mine so here is the link to the authors page, because why to invent something that was already invented. I just modified it a little bit, where Arduini will switch on the red LED when the object/s is out of range and when to switch the green LED on when the object is not out of range. The serial monitor print would be something like in the picture below.


Where the object for example your hand is moving either closer or farther away from the sensor. You can also place an object like cardboard box in front of the sensor and move it slowly away from it. Here is the code bit to calculate the distance of the object.

distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
  Serial.println(“Out of range”);
  digitalWrite(greenPin, LOW);
  digitalWrite(redPin, HIGH);
else {
  digitalWrite(redPin, LOW);
  digitalWrite(greenPin, HIGH);
  Serial.println(” cm”);

Here is few pictures of the wiring itself and again I did the wiring like it is for testing purposes so it was meant to be messy and sorry for the quality of the photo.




And lastly here is the code. I will probably try to decipher the test code and come up with my own code or just make my own functions later but for now this is the loaned code to show the functionality of the HC-SR04 sensor.

#define trigPin 12
#define echoPin 13
const int redPin = 7;
const int greenPin = 8;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);

void loop() {
int duration, distance; // creating variables
digitalWrite(trigPin, HIGH); // pin ON
delayMicroseconds(1000); //delay
digitalWrite(trigPin, LOW); // pin OFF
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println(“Out of range”);
digitalWrite(greenPin, LOW);
digitalWrite(redPin, HIGH);
else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
Serial.println(” cm”);


In the setup method we start the serial port for displaying the status of the sensor. We also define the bits per second.

“Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates – for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.

An optional second argument configures the data, parity, and stop bits. The default is 8 data bits, no parity, one stop bit.”

Where the sensor pins are given constant values with defined names.

"#define is a useful C component that allows the programmer to give a name to a constant value before the program is compiled. Defined constants in arduino don’t take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time.

This can have some unwanted side effects though, if for example, a constant name that had been #defined is included in some other constant or variable name. In that case the text would be replaced by the #defined number (or text).

In general, the const keyword is preferred for defining constants and should be used instead of #define.”

pinIn() means according to arduino website.

“Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds. Gives up and returns 0 if no pulse starts within a specified time out.

The timing of this function has been determined empirically and will probably show errors in longer pulses. Works on pulses from 10 microseconds to 3 minutes in length.”

With the digitalWrite() we either give the pin values HIGH (On) or LOW (Off).


Make: Arduino Bots and Gadgets by Tero and Kimmo Karvinen

BUS4TN007-3 / tehtävä 3 (assignment 3)

This assignment was to make Parallax FlexiForce to work on Arduino Mega 2560. This was a bit trickier to build. The items you will need for this are

– 3 or 4 jumper wires
– Resistor (red, red orange, gold)  = 22,000 ohms ± 5%
– Arduino Mega 2560
– Parallax Flexiforce
– Arduino/computer USB connector
– Breadboard

Now before you start, please take a look at my previous assignments to be able to install the environment needed for the code and look up the logic behind Arduino functions. I also suggest to look up for the color codes for the resistors and the Flexiforce’s datasheet. Another thing I suggest you to search, is the symbols used in electronics and what they mean so you will easily understand what is going on when you read a map like the one I have posted below.


The logic behind the Flexiforce is the above picture. You might always see different tutorials in electronics but I always recommend you to look for the original data sheet and recommendation because I have always noticed that even if the tutorials had the working version of the electronic, a lot of them had problems in their wiring  Even I have some illegal wiring’s in my electronics every now and then.

Now in the Flexiforce you might see three pins but the middle pin under the Flexiforce is useless, because it is only used to keep the sensor down and steady. The sensor does not really have fixed GND/Ground and electricity input and output pins so use your common sense when making wiring. The picture is below.


UPDATE: I have to add that in the upper pictures the wiring is a bit silly because in the Flexiforce sensor there is only two pins that are usable. As you can see, for some reason my group wired the middle pin to Arduino. A good reason to use common sense, because there was nothing on the Flexiforce itself that would have indicated that the middle pin is in use, as you can see on the datasheets. There is nothing connected to the pin.


So the sensor as I mention before (The tall tower like figure) is Flexiforce where by pinching the head of the sensor, the sensor will give values based on your wiring meaning that it will either have values starting from 0 to up or the other way around. If you look at the datasheet, you will see that by what logic does the sensor work.

The code is below

int sensor;

void setup()



void loop()

sensor = analogRead(0);

Serial.print(“Sensor = “);




Make: Arduino Bots and Gadgets by Tero Karvinen