Posts with «arduino» label

HC-SR04 Ultrasonic Sensor


Introduction:

The HC-SR04 Ultrasonic Sensor is a very affordable proximity/distance sensor that has been used mainly for object avoidance in various robotics projects . It essentially gives your Arduino eyes / spacial awareness and can prevent your robot from crashing or falling off a table. It has also been used in turret applications, water level sensing, and even as a parking sensor. This simple project will use the HC-SR04 sensor with an Arduino and a Processing sketch to provide a neat little interactive display on your computer screen.



Parts Required:
Freetronics Eleven or any compatible Arduino.
HC-SR04 Ultrasonic Sensor
Mini Breadboard 4.5cm x 3.5cm
Protoshieldand female header pins (not essential - but makes it more tidy)
Wiresto connect it all together




The Video:




The Arduino Sketch:



     The above sketch was created using Fritzing.





Arduino Code:
You can download the Arduino IDE from this site.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
HC-SR04 Ping distance sensor:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8

This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html
on 10 Nov 2012.
*/


#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}

//Delay 50ms before next reading.
delay(50);
}

The code above was formatted using hilite.me





Processing Code:
You can download the Processing IDE from this site.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* The following Processing Sketch was created by ScottC on
the 10 Nov 2012 : http://arduinobasics.blogspot.com/

Inspired by this Processing sketch by Daniel Shiffman:
http://processing.org/learning/basics/sinewave.html

*/
import processing.serial.*;


int numOfShapes = 60; // Number of squares to display on screen
int shapeSpeed = 2; // Speed at which the shapes move to new position
// 2 = Fastest, Larger numbers are slower

//Global Variables
Square[] mySquares = new Square[numOfShapes];
int shapeSize, distance;
String comPortString;
Serial myPort;

/* -----------------------Setup ---------------------------*/
void setup(){
size(displayWidth,displayHeight); //Use entire screen size.
smooth(); // draws all shapes with smooth edges.

/* Calculate the size of the squares and initialise the Squares array */
shapeSize = (width/numOfShapes);
for(int i = 0; i<numOfShapes; i++){
mySquares[i]=new Square(int(shapeSize*i),height-40);
}

/*Open the serial port for communication with the Arduino
Make sure the COM port is correct - I am using COM port 8 */
myPort = new Serial(this, "COM8", 9600);
myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line
}

/* ------------------------Draw -----------------------------*/
void draw(){
background(0); //Make the background BLACK
delay(50); //Delay used to refresh screen
drawSquares(); //Draw the pattern of squares
}


/* ---------------------serialEvent ---------------------------*/
void serialEvent(Serial cPort){
comPortString = cPort.readStringUntil('\n');
if(comPortString != null) {
comPortString=trim(comPortString);

/* Use the distance received by the Arduino to modify the y position
of the first square (others will follow). Should match the
code settings on the Arduino. In this case 200 is the maximum
distance expected. The distance is then mapped to a value
between 1 and the height of your screen */
distance = int(map(Integer.parseInt(comPortString),1,200,1,height));
if(distance<0){
/*If computer receives a negative number (-1), then the
sensor is reporting an "out of range" error. Convert all
of these to a distance of 0. */
distance = 0;
}
}
}


/* ---------------------drawSquares ---------------------------*/
void drawSquares(){
int oldY, newY, targetY, redVal, blueVal;

/* Set the Y position of the 1st square based on
sensor value received */
mySquares[0].setY((height-shapeSize)-distance);

/* Update the position and colour of each of the squares */
for(int i = numOfShapes-1; i>0; i--){
/* Use the previous square's position as a target */
targetY=mySquares[i-1].getY();
oldY=mySquares[i].getY();

if(abs(oldY-targetY)<2){
newY=targetY; //This helps to line them up
}else{
//calculate the new position of the square
newY=oldY-((oldY-targetY)/shapeSpeed);
}
//Set the new position of the square
mySquares[i].setY(newY);

/*Calculate the colour of the square based on its
position on the screen */
blueVal = int(map(newY,0,height,0,255));
redVal = 255-blueVal;
fill(redVal,0,blueVal);

/* Draw the square on the screen */
rect(mySquares[i].getX(), mySquares[i].getY(),shapeSize,shapeSize);
}
}

/* ---------------------sketchFullScreen---------------------------*/
// This puts processing into Full Screen Mode
boolean sketchFullScreen() {
return true;
}

/* ---------------------CLASS: Square ---------------------------*/
class Square{
int xPosition, yPosition;

Square(int xPos, int yPos){
xPosition = xPos;
yPosition = yPos;
}

int getX(){
return xPosition;
}

int getY(){
return yPosition;
}

void setY(int yPos){
yPosition = yPos;
}
}

The code above was formatted using hilite.me

 
 



If you like this page, please do me a favour and show your appreciation :

  Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
Have a look at my videos on my YouTube channel.


 
 

 
 
 



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

Freeform Arduino Bliss

Designer Kimio Kosaka soldered together an entire Arduino using the difficult freeform method.

Read the full article on MAKE

Tropism Well: A Tall Drink of Water

Instead of cricking your neck to drink from a water fountain, what if the fountain cricked its neck for you? Tropism Well is an interactive sculpture that senses when someone is near and bows to pour water into a glass.

MR-808 Brings Vintage Drum Machine to Life

Moritz Simon Geist has made a physical version of the famed TR-808 drum machine.

Read the full article on MAKE

MAKE » Arduino 09 Nov 20:00
arduino  drum machine  max  msp  music  solenoid  tr-808  

New Arduino Micro

Adafruit did a lovely job with the Arduino team on the new official Arduino Micro. Everything is tiny, with a smaller ATmega32u4 and parts on the top and bottom, some the smallest components available-- seriously micro! It's available now for $25.

Adafruit’s got a new board, an official Arduino

There’s a new Arduino in town, this time designed in conjunction with Adafruit. It’s the Arduino Micro, a very neat little board designed for breadboard use.

Ostensibly an upgrade of the long in the tooth Arduino Nano, the new Micro takes all the best features of the new Arduino Leonardo and shrinks them down to a convenient stick of gum-sized package. It’s powered by the ATmega32u4 microcontroller, and with a MicroUSB port is able to emulate keyboards, mice and other USB input devices.

Of course with any microcontroller dev board, comparisons must be drawn between the Arduino Micro and the very popular Teensy USB dev board. Like the Teensy boards (and the new Arduino Leonardo), the Micro is able to function as a USB keyboard or mouse. The Teensys, though, is loaded with LUFA making it able to emulate just about anything from mice, USB audio devices, and MIDI synths.


Filed under: arduino hacks

Netduino Plus 2 offers four times the speed, full round of futureproofing (video)

The original Netduino Plus was a welcome alternative for Arduino developers that had its limits -- even networking was almost a step too far. Secret Labs doesn't want any of us to bump our heads on the ceiling with its just-launched Netduino Plus 2. The networkable, .NET-friendly developer board runs a four times faster 168MHz processor with double the RAM (over 100KB) and six times as much code space (384KB) as its two-year-old ancestor. Having so much headroom lets the team build common OneWire and Time Server code into the firmware; Secret Labs reckons that there's enough space that the Plus 2 can easily grow over time. The ports are just as ready for the future with four serial ports, software control of any add-on shields (including Rev C Arduino shields) and a new header that lets programmers debug both managed and truly native code at once. If the upgrade is sufficiently tempting, project builders just need to spend $60 today to enjoy some newfound freedom.

Continue reading Netduino Plus 2 offers four times the speed, full round of futureproofing (video)

Filed under: Misc

Netduino Plus 2 offers four times the speed, full round of futureproofing (video) originally appeared on Engadget on Thu, 08 Nov 2012 20:48:00 EST. Please see our terms for use of feeds.

Permalink | Email this | Comments

Arduino Micro shrinks your favorite DIY platform down to ridiculous proportions

With the Arduino Leonardo, everyone's favorite hackable microcontroller turned a new page. Now it's time to bring that simplified design and slightly expanded feature set to the rest of the family, including the itty-bitty Arduino Micro. The tiny, embed-friendly board was designed with help from Adafruit Industries, one of the biggest players in the DIY market. At the heart of the Micro is the same 16MHz ATmega32u4 chip that powers the Leonardo, which means all the necessary USB controls are baked into the processor. Obviously, the layout here is different, so you wont be mounting the Micro to any shields, but with 20 digital I/O pins, 12 analog input channels and seven PWM channels, there's plenty of room for wiring up your own expansions. Amazingly it crams all that capability in a package just 48mm long and 18mm wide. The Arduino Micro will be available exclusively through Radio Shack and Adafruit first before becoming more widely available next month. The board is available with headers for €21 (roughly $23) and without headers for €18 (about $27). For more, check out the PR after the break.

Continue reading Arduino Micro shrinks your favorite DIY platform down to ridiculous proportions

Filed under: Misc

Arduino Micro shrinks your favorite DIY platform down to ridiculous proportions originally appeared on Engadget on Thu, 08 Nov 2012 20:08:00 EST. Please see our terms for use of feeds.

Permalink | Email this | Comments

KegDroid makes drinking beer more fun

Are you bored with just drinking beer? Are your friends constantly sneaking into your house and stealing your sacred beverages? If so, perhaps you need KegDroid – the Android controlled beer tap created by [Paul Carff].

Looking for a way to add more excitement to drinking his beer, [Paul] spiced up his tap with a little extra technology. He added an Android tablet for touchscreen navigation of the menus, an Arduino to control the flow sensors and solenoid valves, and an NFC reader to act as security for restricted access.  Users must be authenticated before they are allowed to pour any alcohol.

Your name and photo are pulled from your Google+ account as you’re logged in, then you simply select your beverage of choice, and if you’d like a one, eight, or twelve ounce pour. Flow sensors automatically shut off when you have the desired quantity.

Seems like you get more foam than beer, but all in all it’s a cool bar top app.

Check out the video after the break.


Filed under: android hacks, arduino hacks, beer hacks

Meet the Rovera: A MAKE Hangout on Air at 2pm PST/5pm PST Today!

Today marks the debut of the bimonthly "editor's choice" hangout on air, live and online on Google+. This week MAKE editorial director Gareth Branwyn will discuss robots and the Rovera, a new Arduino-powered robot now available in the Maker Shed. As we typically do, we'll announce a special offer during the hangout.

Read the full article on MAKE