You are on page 1of 12

Ultrasonic sensor

Posted on March 29, 2014 by ellen


Reply

The following code outputs the distance:

1. #define echoPin 11 // Echo Pin


2. #define trigPin 12 // Trigger Pin
3.
4. long duration, distance; // Duration used to calculate
distance
5.
6. void setup() {
7. Serial.begin (9600);
8. pinMode(trigPin, OUTPUT);
9. pinMode(echoPin, INPUT);
10. }
11.
12. void loop() {
13. digitalWrite(trigPin, LOW);
14. delayMicroseconds(2);
15.
16. digitalWrite(trigPin, HIGH);
17. delayMicroseconds(10);
18.
19. digitalWrite(trigPin, LOW);
20. duration = pulseIn(echoPin, HIGH);
21.
22. //Calculate the distance (in cm) based on the speed
of sound.
23. distance = duration/58.2;
24.
25. Serial.println(distance);
26.
27. delay(50);
28. }

Posted in Uncategorized | Tagged sensor, ultrasonic | Leave a reply

Photoresistor controlling LED

Posted on February 24, 2013 by ellen


2

With a photo resistor you can control something from your Arduino board depending on light
intake. The following circuit shows how to connect your photo resistor to the Arduino board and
read the values.
1. int ledPin = 3;
2. int photocellInput = 0;
3.
4. void setup() {
5. pinMode(ledPin, OUTPUT);
6. }
7.
8.
9. void loop() {
10.
11. photocellInput = (analogRead(0)/4); // Divides input
0-1023 to resemble to 0-255
12.
13. analogWrite(ledPin, photocellInput);
14. // The delay can be change to get the desired
dimming effect
15. delay(20);
16. }

Posted in circuits | Tagged LED, photoresistor | 2 Replies

Decision maker with LEDs

Posted on November 25, 2012 by ellen


7
The following circuit and code shows an example of a decision maker made with LEDs. Pushing
the button will make the LEDs flash in a random order for 1 second. Then displaying the random
decision where one LED lights up for 3 seconds and then reseting.

1. int timeShowRandom = 1000;


2. int timeShowDecision = 3000;
3. int timeBlink = 50;
4. int buttonPin = 3;
5.
6. int buttonPress = false;
7. int randomNumber;
8. int previousNo = 0;
9. int timePassed = 0;
10.
11. void setup() {
12. // Set button pin
13. pinMode(buttonPin, INPUT);
14. // Set output pins
15. pinMode(12, OUTPUT);
16. pinMode(11, OUTPUT);
17. pinMode(10, OUTPUT);
18.
19. }
20.
21. void getRandomNo() {
22. int rand = random(10,13);
23. if(rand == previousNo) {
24. getRandomNo();
25. } else {
26. randomNumber = rand;
27. previousNo = randomNumber;
28. }
29. }
30.
31. void loop() {
32. // Check if button is pressed
33. if(digitalRead(buttonPin) == HIGH && buttonPress ==
false) {
34. buttonPress = true;
35. } if(buttonPress == true && timePassed <=
timeShowRandom) {
36. getRandomNo(); // Get random pin number
37. digitalWrite(randomNumber, HIGH);
38. delay(timeBlink);
39. digitalWrite(randomNumber, LOW);
40. delay(timeBlink);
41. timePassed = timePassed + (timeBlink * 2);
42. } else if(buttonPress == true) {
43. digitalWrite(random(10,13), HIGH); // Set random
pin on
44. delay(timeShowDecision); // For x seconds
45. buttonPress = false; // Set button to be enabled
again
46. timePassed = 0;
47. } else {
48. // Reset all output pins
49. digitalWrite(10, LOW);
50. digitalWrite(11, LOW);
51. digitalWrite(12, LOW);
52. }
53. }

Posted in Uncategorized | Tagged button, LED, resistor | 7 Replies

Detect switch on digitalread

Posted on June 1, 2012 by ellen


2
1. const int buttonPin = 3;
2. int buttonState = 0;
3.
4. void setup() {
5. Serial.begin(9600);
6. pinMode(buttonPin, INPUT);
7. }
8.
9. void loop(){
10. buttonState = digitalRead(buttonPin);
11. Serial.println(buttonState);
12. }

Posted in Uncategorized | Tagged button, resistor, switch | 2 Replies

Flexi force sensor

Posted on May 29, 2012 by ellen


5

How to hook up a Flexi Force sensor with Arduino and read out the values.

1. int fsrReading;
2.
3. void setup(void) {
4. Serial.begin(9600);
5. }
6.
7. void loop(void) {
8. fsrReading = analogRead(A0);
9. Serial.print("Analog reading = ");
10. Serial.println(fsrReading);
11.
12. delay(100);
13. }

See example of how to use a Flexi Force sensor with Arduino.

Posted in Uncategorized | Tagged flexi force, resistor | 5 Replies


Arduino Ethernet Shield hooked up to LED message display

Posted on January 18, 2012 by ellen


8

This hack allows you to display messages to a standard Amplus LED Message Display from Clas
Ohlsson. The display is controlled by a remote control and via a RJ14 cable plugged into the
screen. Serial data is transmitted from cable to the screen. The LED Message display expects the
message you pass to it to be encrypted with a check sum. With the help of Rasmus blog post I
could generate this checksum in Arduino. I turned his Perl code into Arduino code that you can
see below.

1. int stringToInt(String thisString) {


2. String letters = " !’#$%&’()*+,-
./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[/]^_`abcdefg
hijklmnopqrstuvwxyz{|}~";
3.
4. int newstr = 32 + letters.indexOf(thisString);
5.
6. return newstr;
7. }
8.
9. String generateMessage(String inputString) {
10.
11. byte checksum = 0×74;
12. int i;
13.
14. for(i = 0;i < inputString.length();i++) {
15. String currentChar =
inputString.substring(i,i+1);
16. int asciiChar =
stringToInt(currentChar);
17. checksum = checksum ^ asciiChar;
18. }
19.
20. String hexStr = String(checksum, HEX);
21. hexStr.toUpperCase();
22.
23. String message = "<ID00><L1><PA><FE><MA><WC><FE>" +
inputString + hexStr + "<E>";
24. return message;
25.
26. }
27.
28. String myString = "My led display message here";
29. String mess = generateMessage(myString);
30. Serial.print(mess);

Download my Arduino library on Github to try this out.

Posted in Uncategorized | Tagged Arduino Ehternet Shield, ehternet shield, led message display,
RJ14 cable | 8 Replies

Control servo motor

Posted on January 9, 2012 by ellen


Reply

The servo motor can be controlled by using the servo library(Servo.h) in Arduino IDE. Plug in the
control wire of the servo motor into one of the PWM pins. Attach the servo object to that pin.

1. Servo servoMotor;
2. servoMotor.attach(9);
To rotate the servo motor pass in a number between 0 and 180. The servo motor can only move
up to 180 degrees.

1. myservo.write(90);
2. delay(2000);
3. myservo.write(180);
4. delay(2000);

Posted in Uncategorized | Tagged motor, servomotor | Leave a reply

Control relay with TIP120

Posted on October 26, 2011 by ellen


2

Control Omron G5LE-1 relay that requires 12V to switch the relay on and off. The relay it self is
turning a 220VAC circuit on and off.

Posted in Uncategorized | Tagged diod, Omron G5LE-1, relay, TIP120, VAC | 2 Replies

Transistor controls other power source

Posted on October 26, 2011 by ellen


Reply
Posted in Uncategorized | Tagged 9V battery, c557c, diod, lamp, transistor | Leave a reply

Using a potentiometer to control LED

Posted on October 22, 2011 by ellen


4

You can easly control the current running through your LED by adding a potentiometer as part of
your circuit.
To control the LED with Arduino programming you attach the potentiometer to your analog in and
let your Arduino program decide how much to dim the LED depending on the input you get from
the potentiometer.

The input from analogRead returns a value between 0 and 1023. The analogWrite takes values
between 0 and 255. The code below show you have to convert your analog in value to make your
LED shine as bright as possible when the potentiometer is fully on.

1. int ledPin = 3;
2. int potentiomenterInput = 0;
3.
4. void setup() {
5. pinMode(ledPin, OUTPUT);
6. }
7.
8.
9. void loop() {
10.
11. potentiomenterInput = (analogRead(0)/4); // Divides
input 0-1023 to resemble to 0-255
12.
13. analogWrite(ledPin, potentiomenterInput);
14. // The delay can be change to get the desired
dimming effect
15. delay(20);
16. }

Posted in circuits | Tagged LED, potentiometer | 4 Replies

You might also like