Monthly Archives: January 2014

Sound and pitch change with Arduino

Warning: this is a really really annoying little project. If you want to irritate everyone around you, then this project is for you.

My initial idea was to connect more than one pressure sensor to it and make it like a miniature piano. But I could only find one pressure sensor so I made do with it. 




The code I used for this is really simple. I used the tone() function to code for what would normally be 5 lines of regular code.

int speakerPin = 12;
int pressurePin = 0;
 void setup()
 void loop()
  int reading = analogRead(pressurePin);
  int pitch = 200 + reading / 4;
  tone(speakerPin, pitch);
When you apply pressure to the sensor, the pitch of the note will change. It’s really annoying though so use with caution. 

Arduino: Robotic Wrist using 2 servos and a speaker

I’m currently working on a robotic arm that I hope will have fine motor control abilities.
So far, I’ve got the wrist movements down: I can move vertically and rotate. This is all controlled by an old SONY TV remote.
The vertical movements are controlled by one servo (in code: servoV) and the rotating movements are controlled by another (in code: servo). I’ve got buttons on the remote that do both macro and micro movements.
The speaker lets me know when I’ve exceeded arbitrarily selected max and min degree values (well not so arbitrary: I selected them because I noticed that the servos struggle past certain degrees).

Watch the Demo here! (less than 2 mins)

I’ve provided download links to all the materials you’ll need, but if for some reason they don’t work, download the files from my fritzing project profile:
Robotic Wrist with Speaker (at work #2)

To recreate my robotic wrist you’ll want to:

1 – copy and adapt my code in Arduino
2 – build the physical robot body with servos
3 – connect the servos, speaker, breadboard, and arduino following my circuit diagram

Parts used:
2 servos
1 IR receiver
1 Piezo Speaker
1 breadboard
1 Arduino
1 IR remote
1 9V battery
extra black and red wires
a USB cord that plugs into the Arduino
Some foam core, tape, and a large one of these clips 

1: Copy and adapt my code in Arduino:
Code: Arduino (.ino) file    or    Code: Java (.java) document
Labelled (with results values) JPG of SONY Remote Control  ** you don’t really need this 
** if you use the Arduino code, make sure you place it in a folder that has the exact same name as the .ino file. If you don’t know what I mean, when you try to open the .ino file, Arduino will ask you if it’s okay to put it in a folder. Let it do that **

2: Build the Physical robot body
Watch the video here (2:30 mins)

3: Connect the pieces
Circuit diagram: fritzing (.fzz) document    or    Circuit diagram: PNG


** note: If you’re using larger servos (like I am), make sure your power supply is sufficient. You’ll know it’s insufficient if the intro to Beethoven’s 5th Symphony repeatedly plays. This is because when the power supply is insufficient, the Arduino will keep rebooting, so it will repeatedly run through the setup.

Light up different LEDs under different light intensity

I built a little fun device with Arduino that uses a light sensor to detect how light the ambience is and lights up one of the LEDs accordingly. If it is bright in the surroundings the green LED will light, if it is dim the yellow LED will light and the red LED lights if it is completely dark.

Here is the circuit diagram for setting up the device


Each LED has a wire connected to the UNO board to slot 9, 10, 11 individually, and the resistor and light sensor(photocell) all ultimately connect to the negative ground. Make sure they are all interconnected.

Below is the code with detailed comments:

// LDR reads the ambient light, and lights up one of the three LEDs,
//depending on how light the ambience is

#define LEDg 11
#define LEDy 10
#define LEDr 9
#define SENSOR 0

int val = 0;
int val_sensor = 0;

void setup() {

pinMode(LEDg, OUTPUT);
pinMode(LEDy, OUTPUT);
pinMode(LEDr, OUTPUT);

//when plugged in, all the LED will light at first
digitalWrite(LEDg, HIGH);
digitalWrite(LEDy, HIGH);
digitalWrite(LEDr, HIGH);

void loop() {
val = analogRead(0);

//print out the readings for light intensity from the sensor

//default all the LED values to LOW so at first they won’t light

digitalWrite(LEDg, LOW);
digitalWrite(LEDy, LOW);
digitalWrite(LEDr, LOW);

 //bright surroundings light the green LED

if (val >= 500)
digitalWrite(LEDg, HIGH);

//medium light intensity, light the yellow LED
else if (val > 200)

digitalWrite(LEDy, HIGH);


//when the light intensity is VERY LOW, light the red LED

digitalWrite(LEDr, HIGH);

//set up the delay to 100 milliseconds so the LED respond to change in the light intensity faster

Measuring temperature with Arduino

Hi guys!

Here’s a fun little thing you can do with your Arduino. If you don’t have a thermometer but would still like to know the temperature in your room, you can build one using the temperature sensor found in the basic kits for Arduino. 



I had to turn the sensor around in this diagram so you could actually see where the wires go. For pin 1 (which when the sensor faces you with the flat side is the left most one, but in this it’s the right most one), you will attach your power line (in this case 5V). Pin 2 goes to an analog source (A1 in my case) and number 3 is your ground pin.

Arduino code:

int tempPin =1; //this is the analog pin you’re connected to

void setup()
{Serial.begin(9600); //serial port connection to the computer


void loop()

int tempRead = analogRead(tempPin); //voltage reading from sensor
Serial.println(tempRead); //puts out the reading in the serial monitor
int tempC = map(tempRead, 0, 1023, -50, 450); //your values will be from 0 to 1024 which you then convert to your degrees using the map function

//10mv correspond to 1 degreeC
Serial.print(“new temp: “);


Color certain part of the photo

Want to make your photo fancier? Here is a little useful trick

This is the original image:


If we want to color only the flower and make the rest black and white, it will look like this:




Open the image and select ‘Pen Tool’ from the tool barImage, the select ‘Path’ from the function window at the topImage, and draw along the figure of flower like this



press ‘CTRL+ENTER’ to select the area you just drew, and then press ‘CTRL+SHIFT+I’ to select the counterpart



Duplicate the background layer to get background layer copy, then select ‘<Image –> Adjustments –> Hue/Saturation>, a window will pop up and enter the data like following



Now your image will look like this:


You are done for now, but if you want to further change the color of the flower, do the following:

press ‘CTRL+SHIFT+I’ to counter-select the flower part, and get the window for ‘Hue/Saturation’ again( A fast way to do this is to press ‘CTRL+U’)

If I want the flower in bright color, I will enter the data as following



Notice the color of flower changed? Press ‘CTRL+D’ to cancel selection, and you are done!


Making mirrored image

We also see fancy cosmetics commercials like this:





Using photoshop makes the product looks more presentable, and you may find the trick is very useful in photo-processing. Wonder it is done? Here is the simple tutorial:

First, we need to set up the background, click ‘File –> New’, in the window popped up, entry the data


Note the entry could be different depends on what size and background color you want, for the example above, we get a image like this:



Open the target image, select ‘Magic Wand Tool’Image, click at the background , press ‘CTRL+SHIFT+I’ to select the counterpart



Drag the selected image to the background image



Use the same method to get the bottle image



Now we want to make the mirrored images, selected ‘Layer 1’ and ‘Layer 2’ and duplicate them, and we get ‘Layer 1 copy’ and ‘Layer 2 copy’


Select these 2 copies of layers and press ‘CTRL + T’ to transform them,  right click on the image and select ‘Flip Vertical’, we get the image like this



Adjust the opacity of this two copies of layer to 40%



and we get the result image, YEAH~


Fixing black and white drawings in Photoshop

Hey guys, here’s a quick tutorial on how I personally fix my drawings using photoshop. I hate that my portraits look real nice and crisp in real life, but once they get scanned, it’s like all the black got sucked out of them and I’m left with a greyed out mess that doesn’t look nearly as nice as the original. Luckily, you can use photoshop to tweak some things and make your drawings look more like you want them.

For this tutorial, I’m going to use this portrait I did of my friend Jon. 



As you can see, the scanner did a weird job of the proportions, and the colors look off and patchy. What I first did was use the Rectangular Marquee Tool and made a selection of the part of the drawing I wanted to keep and pasted it into a new window (that way I got rid of the white margin that came out from the scanner going over the page). Next, I used the Free Transform option to rescale the image so it wasn’t so stretched out.



Much better in terms of proportions! Next, let’s try to fix the color. In the original, the background was pitch black, but some of the color lifted in the scan. Next thing is to duplicate the layer (right click the current layer tab and select Duplicate layer). This way we can make adjustments and go back to the original if we don’t like it.

For black and white drawings, I like to go to Image> Adjustments> Black and White. This way when we mess around with the levels and curves we don’t get a weird green cast. But this still doesn’t bring the black back into the drawing.To bring it back, we can use the nifty Levels options. 




Dragging the arrows around allows you to lighten and darker the tones of your pictures. I like to drag the left most one quite a bit in, which effectively darkens the background and shadows that otherwise wouldn’t be showing. 


After tweaking the arrows for a while, this was my end result. More techniques could be used, like using the Burn tool to further darken some areas to make them blend better, like the shadows in the hair. But for now, this will do. 



The finished product.

I hope you learned something new and hopefully this will be useful for you when you’re trying to fix your scans!