Arduino Projects: Difference between revisions

 
(22 intermediate revisions by the same user not shown)
Line 91:
 
= Versatile Kitchen Timer =
 
{{notice|This code can be re-wired using capacitative touch sensor.}}
 
[[File:Kitchen Versatile Timer.jpg|thumb|right|Kitchen Versatile Timer]]
 
<pre style="width: 75%; height: 10pc; overflow-y: scroll;">
//This code is written by Amandeep Singh
Line 157 ⟶ 160:
}
}
}
</pre>
 
= Light Controlled Relay Switch =
[[File:LDR Relay bb.png|thumb|right|LDR Relay Switch]]
<pre style="width: 75%; height: 10pc; overflow-y: scroll;">int sensorPin = A0; // select the input pin for ldr
int sensorValue = 0; // variable to store the value coming from the sensor
 
void setup() {
pinMode(2, OUTPUT); //pin connected to the relay
Serial.begin(9600);
}
 
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
if(sensorValue < 700) //setting a threshold value
digitalWrite(2,HIGH); //turn relay ON
else digitalWrite(2,LOW); //turn relay OFF
delay(100);
}
</pre>
Line 438 ⟶ 465:
http://circuitdigest.com/microcontroller-projects/making-3X3X3-led-cube-with-arduino
 
 
= Bluetooth Controlled RC Car =
Source: [http://www.instructables.com/id/Arduino-Bluetooth-RC-Car-Android-Controlled/ instructables.com], [http://www.instructables.com/id/Control-DC-and-stepper-motors-with-L298N-Dual-Moto/ instructables.com]
 
[[File:Arduino Bluetooth RC Car bb.png|thumb|right|Arduino Bluetooth RC Car]]
 
<pre style="width: 75%; height: 10pc; overflow-y: scroll;">const int motor1Pin1 = 10;
const int motor1Pin2 = 11;
const int motor2Pin1 = 6;
const int motor2Pin2 = 5;
const int led = 8;
const int buzzer = 12;
int i = 0;
 
byte serialA;
 
 
void setup() {
Serial.begin(9600);
 
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
}
 
void alarm()
{
for (int i=0; i<2; i++) //alarm will ring for 2 seconds once triggered
{
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
delay(100);
}
}
 
 
void loop() {
if (Serial.available() > 0)
{
serialA = Serial.read();
Serial.println(serialA);
}
switch (serialA) {
// forward
case 'F':
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
break;
// left
case 'L':
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
break;
// right
case 'R':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
break;
// forward left
case 'G':
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
break;
// forward right
case 'I':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
break;
 
// backward left
case 'H':
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
break;
// backward right
case 'J':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
break;
// backward
case 'B':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
break;
// Stop
case 'S':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
break;
case 'W':
digitalWrite(led, HIGH);
break;
case 'w':
digitalWrite(led, LOW);
break;
case 'V':
alarm();
break;
}
}
</pre>
 
=Bluetooth RC Car with Speed Control=
{{notice|Need to implement the Speed control using the enable pins on L298N instead of the Input Pins}}
[[File:Arduino Bluetooth RC Car bb.png|thumb|right|Arduino Bluetooth RC Car]]
<pre style="width: 75%; height: 10pc; overflow-y: scroll;"> const int motorA1 = 11; // L293 Connection Pin 2 of L293
const int motorA2 = 10; // Pin 7 of L293
const int motorB1 = 6; // Pin 10 of L293
const int motorB2 = 5; // Pin 14 of L293
 
const int lights = 13; //Leds connected to Arduino UNO Pin 12
const int buzzer = 8 ; //Buzzer,Speaker to Arduino UNO Pin 3
const int BTState = 7; //Bluetooth (HC-06 JY-MCU) State pin on pin 2 of Arduino
//Calculate Battery Level
const float maxBattery = 8.0; // Change value to your max battery voltage level!
int perVolt; // Percentage variable
float voltage = 0.0; // Read battery voltage
int level;
// Use it to make a delay... without delay() function!
long previousMillis = -1000*10;// -1000*10=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec.
long interval = 1000*10; // interval at which to read battery voltage, change it if you want! (10*1000=10sec)
unsigned long currentMillis; //unsigned long currentMillis;
 
int i=0; //Useful Variables
int j=0;
int state;
int vSpeed=200; // Default speed, from 0 to 255
 
void setup() {
pinMode(motorA1, OUTPUT); // Set pins as outputs:
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
pinMode(lights, OUTPUT);
pinMode(BTState, INPUT);
Serial.begin(9600); // Initialize serial communication at 9600 bits per second:
}
void loop() {
if(digitalRead(BTState)==LOW) { state='S'; } //Stop car when connection lost or bluetooth disconnected
if(Serial.available() > 0){ //Save income data to variable 'state'
state = Serial.read();
}
if (state == '0'){ //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
vSpeed=0;}
else if (state == '1'){
vSpeed=100;}
else if (state == '2'){
vSpeed=200;}
else if (state == '3'){
vSpeed=300;}
else if (state == '4'){
vSpeed=400;}
else if (state == '5'){
vSpeed=500;}
else if (state == '6'){
vSpeed=600;}
else if (state == '7'){
vSpeed=700;}
else if (state == '8'){
vSpeed=800;}
else if (state == '9'){
vSpeed=900;}
else if (state == 'q'){
vSpeed=1000;}
Serial.println(vSpeed);
Serial.println(state);
/***********************Forward****************************/
//If state is equal with letter 'F', car will go forward!
if (state == 'F') {
analogWrite(motorA1, vSpeed);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);
}
/**********************Forward Left************************/
//If state is equal with letter 'G', car will go forward left
else if (state == 'G') {
analogWrite(motorA1, vSpeed);
analogWrite(motorA2, 0);
analogWrite(motorB1, 200);
analogWrite(motorB2, 0);
}
/**********************Forward Right************************/
//If state is equal with letter 'I', car will go forward right
else if (state == 'I') {
analogWrite(motorA1, vSpeed);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 200);
}
/***********************Backward****************************/
//If state is equal with letter 'B', car will go backward
else if (state == 'B') {
analogWrite(motorA1, 0);
analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);
}
/**********************Backward Left************************/
//If state is equal with letter 'H', car will go backward left
else if (state == 'H') {
analogWrite(motorA1, 0);
analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 200);
analogWrite(motorB2, 0);
}
/**********************Backward Right************************/
//If state is equal with letter 'J', car will go backward right
else if (state == 'J') {
analogWrite(motorA1, 0);
analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0);
analogWrite(motorB2, 200);
}
/***************************Left*****************************/
//If state is equal with letter 'L', wheels will turn left
else if (state == 'L') {
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 200);
analogWrite(motorB2, 0);
}
/***************************Right*****************************/
//If state is equal with letter 'R', wheels will turn right
else if (state == 'R') {
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 200);
}
/************************Lights*****************************/
//If state is equal with letter 'W', turn leds on or of off
else if (state == 'W') {
if (i==0){
digitalWrite(lights, HIGH);
i=1;
}
else if (i==1){
digitalWrite(lights, LOW);
i=0;
}
state='n';
}
/**********************Horn sound***************************/
//If state is equal with letter 'V', play (or stop) horn sound
else if (state == 'V'){
if (j==0){
tone(buzzer, 1000);//Speaker on
j=1;
}
else if (j==1){
noTone(buzzer); //Speaker off
j=0;
}
state='n';
}
/************************Stop*****************************/
//If state is equal with letter 'S', stop the car
else if (state == 'S'){
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);
}
/***********************Battery*****************************/
currentMillis = millis(); //Read battery voltage every 10sec.
if(currentMillis - (previousMillis) > (interval)) {
previousMillis = currentMillis;
voltage = (analogRead(A0)*5.015 / 1024.0)*11.132; //Read voltage from analog pin A0 and make calibration
//Calculate percentage...
perVolt = (voltage*100)/ maxBattery;
if (perVolt<=75) { level=0; }
else if (perVolt>75 && perVolt<=80) { level=1; } // Battery level
else if (perVolt>80 && perVolt<=85) { level=2; } //Min ------------------------ Max
else if (perVolt>85 && perVolt<=90) { level=3; } // | 0 | 1 | 2 | 3 | 4 | 5 | >
else if (perVolt>90 && perVolt<=95) { level=4; } // ------------------------
else if (perVolt>95) { level=5; }
Serial.println(level);
}
}
</pre>
<br />
 
= Obstacle Avoiding Robot =
 
{{UC}}
 
= Bluetooth Controlled RGB LED Strip =
Source: [https://www.arduino.cc/en/Tutorial/ReadASCIIString arduino.cc], [https://play.google.com/store/apps/details?id=com.embarcadero.AndroidunoBt&hl=en play.google.com]
 
Download the AndroidunoBt App from: [https://play.google.com/store/apps/details?id=com.embarcadero.AndroidunoBt&hl=en play.google.com]
 
[[File:Bluetooth Controlled RGB LED Strip.jpg|thumb|right]]
 
*Reading a serial ASCII-encoded string.
Line 624 ⟶ 976:
}
</pre>
 
=Running Lights=
http://www.14core.com/led-running-light-or-chasing-light-effect/
<br />
 
=Running Lights with Speed Control using Potentiometer=
 
Source: [http://arduino.stackexchange.com/questions/19605/running-light-without-delay-and-a-potentiometer stackexchange.com]
 
[[File:Running Lights with Speed Control using Potentiometer.jpg|thumb|right]]
 
<pre style="width: 75%; height: 10pc; overflow-y: scroll;">
// Sketch re: http://arduino.stackexchange.com/questions/19605/running-light-without-delay-and-a-potentiometer
// Set constants for pins with LEDs
enum { led1 = 13, led2 = 12, led3 = 11, led4 = 10, led5 = 9, led6 = 8, led7 = 7, led8 = 6, led9 = 5, led10 = 4};
 
// Make an array with the LED pin numbers
byte ledPins[] = { led1, led2, led3, led4, led5, led6, led7, led8, led9, led10 };
 
// # of entries in ledPins:
enum { numLeds = sizeof(ledPins) / sizeof ledPins[0]};
 
//count to track which LEDs are HIGH and which are LOW
int count = numLeds-1; // Will roll over to 0
 
// To store last time LED was updated
unsigned long previousMillis = 0;
 
void setup() {
// initialize digital pin outputs
for (byte i=0; i<numLeds; ++i)
pinMode(ledPins[i], OUTPUT);
 
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
 
// loop() runs over and over again forever:
void loop() {
unsigned long currentMillis = millis();
// read input on analog pin 0:
int deli = analogRead(A0);
float voltage = deli * (5.0 / 1023.0);
 
// Print out the value and corresponding voltage you read:
// Serial.println("Value: %d and Voltage: %f", pause, voltage);
Serial.print("Value: ");
Serial.println(deli);
Serial.print("Volts: ");
Serial.println(voltage);
 
if (currentMillis - previousMillis >= deli) {
// Save the last time we blinked the LED
previousMillis = currentMillis;
 
// Turn off current LED, turn on next one
digitalWrite(ledPins[count], LOW);
count = (count+1) % numLeds;
digitalWrite(ledPins[count], HIGH);
}
}
</pre>
 
= LED Matrix Clock =
https://123led.wordpress.com/mini-led-clock/
{{UC}}
 
= RF 433 MHz Module =
 
Source: [https://randomnerdtutorials.com/rf-433mhz-transmitter-receiver-module-with-arduino/ randomnerdtutorials.com]
 
*Download the Library file:
https://lastminuteengineers.com/libraries/RadioHead-1.84.zip
 
*Connect the circuit as below:
Arduino Tx Module
5v VCC
GND GND
12 Data
 
Arduino Rx Module
5v VCC
GND GND
11 Data
 
*Transmitter Side Code
<pre>
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
 
RH_ASK driver;
 
void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
 
void loop()
{
const char *msg = "Hello World!";
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(1000);
}
</pre>
 
 
*Receiver Side Code
<pre>
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
 
RH_ASK driver;
 
void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
 
void loop()
{
uint8_t buf[12];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, dump it.
Serial.print("Message: ");
Serial.println((char*)buf);
}
}
</pre>
 
= Motor Shield =
 
Source: [https://lastminuteengineers.com/l293d-motor-driver-shield-arduino-tutorial/ lastminuteengineers.com]
 
*Single DC power supply for both Arduino and motors:
Plug Voltage < 12V into the DC jack on the Arduino or the 2-pin EXT_PWR block on the shield.
Jumper ON
*(Recommended) Arduino via USB and motors via DC power supply:
Arduino plug in the USB cable.
Then connect the motor supply to the EXT_PWR block on the shield.
Jumper OFF
 
*Arduino Jack and motors via DC power supply:
Plug in the supply for the Arduino into the DC jack.
Connect the motor supply to the EXT_PWR block.
Jumper OFF
 
Warning:
DO NOT supply power to the EXT_PWR input when jumper is in place.
It may damage the motor shield and also your Arduino!
 
The shield offers below features:
The shield comes with a pulldown resistor array to keep motors switched off during power-up.
The on-board LED indicates the motor power supply is Okay. If it is not lit, the motors will not run.
The RESET is nothing but Arduino’s reset button. It just brought up top for convenience.
 
<br />
<br />