I tried connecting my components on the breadboard but it is wrong as the reading of my serial monitor keeps displaying 0. I'm trying to get the light bulb to light up within a certain distance, using the components shown in the photo. How do I go about with this?
#include <math.h>
// defines pins numbers
const int trigPin = 10;
const int echoPin = 9;
// defines global variables
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(8, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the ultrasound wave travel time in microsec
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
if (distance <= 280)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print ("Distance(cm): ");
Serial.println(distance);
delay(500);
}
Is that a battery at the lower right? Its - is not connected to all the Gnds.
LED needs a resistor. Looks like 8 is connected to Batt+.
The Pot(?) only has 2 connections - are you just using it as a variable resistor between Batt+ and Gnd? That will just drain the battery.
The light sensor does not appear to be connected.
And, post your code, use the code tags button </>,
I would suggest you generate a schematic first, that will show you a lot of your circuit problems. Once you have that correct then chase the connections on the prototype board. When doing your code do one section at a time and get it working before going on to the next.
I'm trying to make a circuit design a lamp with digital controls but as I press the button, I realise the light of the LED keeps flickering, I'm not sure if it's supposed to be like that?
#include <math.h>
// defines pins numbers
const int trigPin = 10;
const int echoPin = 9;
// defines global variables
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(8, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the ultrasound wave travel time in microsec
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print ("Distance(cm): ");
Serial.println(distance);
delay(500);
if (distance == 280)
{
digitalWrite(8, HIGH);
Serial.println("LED ON");
}
else
{
digitalWrite(8, LOW);
Serial.println("LED OFF");
}
int lightValue = analogRead(A0);
Serial.print("Detected light level: ");
Serial.println(lightValue);
delay(500);
}
On the "bottom" of your diagram your white solderless breadboard have two rows of contacts:
The top one is marked for ground or common or -V (near black line)
The bottom one is marked for +V or Vcc (near red line)
Wire ALL Vcc or +V to the row by the red line
Wire ALL ground or common to the row by the black line.
Also note your relay needs 4 connections you only have two.
Your ultrasonic board is not connected to ground.
You will need to look at each component individually, understand how it works and verify it has what it needs to work.
Also work on small bits at a time. Perhaps starting with the LDR (which also won't work because you need a resistor to ground)
look here using an LDR about 1/2 way down the "page" you will find a schematic of an LDR circuit. Study it, if needed Google "resistor divider". We've all learned the schematic shows the "concept" where the fritz shows the instructions.
Consider googling a location for a hobby shop in your area.
If you go by the step by step instructions you will find your way there but have little idea where you are. (aka Fritz)
If you look at the graphic and see the roads and relative locations will not only be able to get there but will understand were you are (aka Schematic). AND more importantly be able to troubleshoot if you get lost. (works the same with circuits or roads.)