Ultrasonic sensor
The following code outputs the distance:
-
#define echoPin 11 // Echo Pin
-
#define trigPin 12 // Trigger Pin
-
-
long duration, distance; // Duration used to calculate distance
-
-
void setup() {
-
Serial.begin (9600);
-
pinMode(trigPin, OUTPUT);
-
pinMode(echoPin, INPUT);
-
}
-
-
void loop() {
-
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;
-
-
Serial.println(distance);
-
-
delay(50);
-
}
Photoresistor controlling LED
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.
![arduino_photocell](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_tFSvX-xkxZgkn5UB-vYWAzppwsbKWpW3_wEOowUexc6dd996s5-2B7oSpAHcQbQpyCy4rRfwPm5moVHXKjCbnCd84Bfc1dn8-56UeNcl_kJf3NhYW0XJOFdVXsO0UKLFLgscu5fLwzycuRAaVs=s0-d)
-
int ledPin = 3;
-
int photocellInput = 0;
-
-
void setup() {
-
pinMode(ledPin, OUTPUT);
-
}
-
-
-
void loop() {
-
-
photocellInput = (analogRead(0)/4); // Divides input 0-1023 to resemble to 0-255
-
-
analogWrite(ledPin, photocellInput);
-
// The delay can be change to get the desired dimming effect
-
delay(20);
-
}
Decision maker with LEDs
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.
![arduino_sundh_random_leds](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u7aLDeLazqFfd8UtRaxo66Utm6pmqyJNhhoVU1kNVrE1Nk8Rw_BBEJSWsIv-UvC0vMLRtkE_OG_t2Z5wNWQxLpklpPxVUsQlrN51p1HzLVtJHIuao5nlnSAa9T5onzpfcC7x__36slw6yuiARb0E7RTIDOFuA=s0-d)
-
int timeShowRandom = 1000;
-
int timeShowDecision = 3000;
-
int timeBlink = 50;
-
int buttonPin = 3;
-
-
int buttonPress = false;
-
int randomNumber;
-
int previousNo = 0;
-
int timePassed = 0;
-
-
void setup() {
-
// Set button pin
-
pinMode(buttonPin, INPUT);
-
// Set output pins
-
pinMode(12, OUTPUT);
-
pinMode(11, OUTPUT);
-
pinMode(10, OUTPUT);
-
-
}
-
-
void getRandomNo() {
-
int rand = random(10,13);
-
if(rand == previousNo) {
-
getRandomNo();
-
} else {
-
randomNumber = rand;
-
previousNo = randomNumber;
-
}
-
}
-
-
void loop() {
-
// Check if button is pressed
-
if(digitalRead(buttonPin) == HIGH && buttonPress == false) {
-
buttonPress = true;
-
} if(buttonPress == true && timePassed <= timeShowRandom) {
-
getRandomNo(); // Get random pin number
-
digitalWrite(randomNumber, HIGH);
-
delay(timeBlink);
-
digitalWrite(randomNumber, LOW);
-
delay(timeBlink);
-
timePassed = timePassed + (timeBlink * 2);
-
} else if(buttonPress == true) {
-
digitalWrite(random(10,13), HIGH); // Set random pin on
-
delay(timeShowDecision); // For x seconds
-
buttonPress = false; // Set button to be enabled again
-
timePassed = 0;
-
} else {
-
// Reset all output pins
-
digitalWrite(10, LOW);
-
digitalWrite(11, LOW);
-
digitalWrite(12, LOW);
-
}
-
}
Detect switch on digitalread
Flexi force sensor
How to hook up a Flexi Force sensor with Arduino and read out the values.
-
int fsrReading;
-
-
void setup(void) {
-
Serial.begin(9600);
-
}
-
-
void loop(void) {
-
fsrReading = analogRead(A0);
-
Serial.print("Analog reading = ");
-
Serial.println(fsrReading);
-
-
delay(100);
-
}
Control servo motor
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.
-
Servo servoMotor;
-
servoMotor.attach(9);
-
myservo.write(90);
-
delay(2000);
-
myservo.write(180);
-
delay(2000);
Control relay with TIP120
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.
![transistor_TIP120_12V_g5LE-1](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_tH33ueZvowmq2QdQtxcZjrnYax9eS96JRyioV7iawV5f4KAcHqFTPtd4aMXQKSLY74lQ0S7aPRLzA8RDjoLOD7btcNrehS6HhRGuD3Bq_F5lwabvUjKjdN8fH94NvPDtWrwceZI1K6IhNDNCDArTxRHrDkb6104g=s0-d)
Transistor controls other power source
Using a potentiometer to control LED
You can easly control the current running through your LED by adding a potentiometer as part of your circuit.
![potensometer1_bb](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_uUriVE2IyCH8oVXP_13PQfJaDs52W7zi8EpDLVfbYf2xMrJPycS5BknqtA0hWTXbxiOaYVaJksCor46m-DhJqqwsD6rmggq5QkTY-w24Hz4hZKh-fo8CdWaMaYJwICAzQ0t6YafESZbQpA8g=s0-d)
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.
![arduino_potentiometer_led](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_sodvbDKga9M3AWphDoOuZ-7h7lqrAbYV4zyoBmZF2YnPoc5BHQiokGAmk-9sUl9DRSizkMI083CQY_FCNgrqfxOzdJLp4Rs0b1Afr-JmWAw_B6LSykZ2IBS78EWyTGCTyLobs8NjOn0kHSUYGxXJhTQ9KqAw=s0-d)
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.
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.
-
int ledPin = 3;
-
int potentiomenterInput = 0;
-
-
void setup() {
-
pinMode(ledPin, OUTPUT);
-
}
-
-
-
void loop() {
-
-
potentiomenterInput = (analogRead(0)/4); // Divides input 0-1023 to resemble to 0-255
-
-
analogWrite(ledPin, potentiomenterInput);
-
// The delay can be change to get the desired dimming effect
-
delay(20);