Simple Analog Read Arduino - arduino

I am trying to program some software for my Arduino to read data from an IR fire detector module, I know the module works but I just cannot get the Arduino to read from it and carry out a function correctly. I want it to work like this...
const int IRDetector1Input = A2;
const int IRDetector1Output = A1;
const int LEDButton = 3;
void setup(){
pinMode(IRDetector1Input, INPUT);
pinMode(IRDetector1Output, OUTPUT);
pinMode(LEDButton, OUTPUT);
void loop(){ //Problem Point
if analogRead(IRDetectorInput, HIGH); //This is the problem, the code doesn't match with digital or analog write.
digitalWrite(LEDButton, HIGH);

There are a couple of issues that I see.
One, is that you are using the analogRead() function incorrectly. When you perform an analogRead() the function reads a digital representation of the voltage as seen at the requested pin and returns an integer value (0 to 1023). Also, this function only takes a single parameter, the pin number. You are sending the pin number and HIGH. Here is an example of what to do if you wanted to know what the digital representation of the voltage was at that analog pin:
int analogValue;
analogValue = analogRead(IRDetector1Input);
The second issue is with your if() statement. I believe that you are trying to see if the value received at the pin (where your IR detector is connected) is actually HIGH. First, your if() statement would be more correct (but still wrong), like this:
if (analogRead(IRDetector1Input) == HIGH)
digitalWrite(LEDButton, HIGH);
The reason that it is still wrong is that HIGH is a value that is defined as being the integer number 1, whereas LOW is defined as 0. So, you would be comparing the return value of analogRead() which could be anywhere from 0 to 1023, to the number 1.
So... how can you fix it? Well, it's tough to say without knowing how your IR detector module works. If the detector sends a digital value (a high voltage or a low voltage and not something in between) to your Arduino, use digital functions and a digital pin (i.e. digitalRead()). However, if your IR is actually sending an undetermined voltage, then set an analog threshold and check for it. You will need to run tests to determine where this threshold should be. For example:
#define ANALOG_IR_THRESHOLD 750 /* Arbitrarily set */
if (analogRead(IRDetector1Input) >= ANALOG_IR_THRESHOLD)
{
digitalWrite(LEDButton, HIGH);
}

Related

My arduino nano analogRead always return 1023

The CDS sensor module connected to the Arduino Nano returns only a value of 1023.
my code hear
int Cds = A0;
//int Led = 13;
int value;
void setup() {`enter code here`
Serial.begin(9600);
pinMode(Cds,INPUT);
//pinMode(Led,OUTPUT);
}
void loop() {
Cds=analogRead(A0);
Serial.println(Cds);
if(Cds<300)
Serial.println("dark");
else
Serial.println("bright");
delay(1000);
}
and nano is connected breadboard
Check analogReference() in the code for your board to configures the reference voltage used for analog input. If you have have correct reference voltage setup you get 1023 max value all the time.
Firstly, the first line in loop() should be changed to:
value = analogRead(Cds)
And from there, just use value for accessing the brightness, because Cds is just your alias for A0
The second possible Error source could be this:
If you just connect the module to GPIO and GND, nothing will happen, and it will always read the maximum value (1023).
You need to use it like a voltage divider, to read values which are relative to a certain voltage
If you connect everything like this, it should work.

Arduino to generate a rising waveform (sine or triangle)

I would like to apply an increasing voltage and hold to my output from my arduino UNO. I realize that the arduino does not allow me to output analog values, and thus I decided to use an R2R ladder (with R- 22kohms and 2R- 47kohms). This would allow me to convert to an analog voltage. I made use of the eight digital pins on the arduino, to set up an 8 bit R2R ladder. I am able to output a sine wave, with my current setup, but a little bit unsure on how to output a wave which goes up to the maximum value and stops. (i.e. a wave like given in the picture below).
This wave is basically a triangle wave or even a sine wave which goes up to a max value and stays there (with 200 micro second pulse duration).
I have created a visual of my circuit to better demonstrate my problem:
I also attempted my problem, by outputting a sine wave. My code is as follows:
void setup() {
//set pins 0-7 as outputs
for (int i=0; i<8; i++){
pinMode(i, OUTPUT);
}
}
void loop() {
double value =0;
int check=0; int t=0;
while(check==0){
if (value<254){
value = 127+127*sin(2*3.14*t/100);
//this sends a sine wave centered around (127/255 * 5)=2.5V
//max will reach when t=25
PORTD=value;
delayMicroseconds(4); //wait 4 micro seconds
//this means that the max value will reach at ~25*6 =150 microseconds
}
else{
value =255;
PORTD=value; //just output the max of the sine wave (i.e. 255)
delayMicroseconds(50); //delay to ensure total duration is 150+50=200 microseconds
PORTD=0; //output back a 0
check=1; //condition to exit the loop
}
t=t+1;
}
}
For some reason, the pulse generated is not exactly what I am looking for. Is there something I am doing wrong? Or is there a better implementation for something like this? Additionally, if there is something I am missing in my question, please let me know.
I realize that the arduino does not allow me to output analog values
In order to output analog values use one of the analog outputs of the Arduino.
They are marked with a ~
Here' an example from the Arduino reference:
int ledPin = 9; // LED connected to digital pin 9
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value
void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}

Arduino Mini Pro 3.3V does not use 1.1V internal reference for ADC

Using the Arduino Mini Pro 3.3V I just stumbled over a problem when switching between the "INTERNAL" and "DEFAULT" voltage reference for the ADC.
I want to measure the output of a voltage divider [GND - 110kOhm - A2 - 500kOhm - VCC] for calculating VCC. VCC has been measured as 3.3V. It is provided by a voltage regulator.
In the loop I firstly measure the voltage divider output with the internal reference and afterwards with the default voltage reference.
I saw code examples where people recommend to wait some milliseconds before reading the next value and the (analogReference() documentation) recommends to ignore the first readings after calling analogReference(). I follow these guidlines.
I'll provide a minimum example sketch:
// the setup function runs once when you press reset or power the board
void setup()
{
pinMode(A2, INPUT); // ADC pin
Serial.begin(9600);
Serial.println("----------------");
}
void burn8Readings(int pin)
{
for (int i = 0; i < 8; i++)
{
analogRead(pin);
}
}
// the loop function runs over and over again forever
void loop()
{
uint16_t nResult1, nResult2;
analogReference(INTERNAL); // set the ADC reference to 1.1V
delay(10); // idle some time
burn8Readings(A2); // make 8 readings but don't use them to ensure good reading after ADC reference change
nResult1 = analogRead(A2); // read actual value
analogReference(DEFAULT); // set the ADC reference back to internal for other measurements
delay(10); // idle again
burn8Readings(A2); // make 8 readings but don't use them to ensure good reading after ADC reference change
nResult2 = analogRead(A2); // do other measurements
// print result to serial interface..
Serial.print("1: ");
Serial.print(nResult1);
Serial.print(" - 2: ");
Serial.println(nResult2);
delay(2000);
}
The first pair of ADC results seems correct (553 / 184), but in the following iterations the first value is faulty without changing the actual voltage on the ADC pin. (240 / 183)
The ADC result of the DEFAULT reference is always fine.
For a 2.56V reference the value of 240 would be feasible. I know that some ATmegas use a 2.56V reference voltage, but the ATmega328 should have 1.1V only. Strangely the (ATmega328/P datasheet) mentions a 2.56V reference in an ADC example in chapter 28.7, so I'm confused.
Is there a possibility there is a 2.56V ADC reference in a certain ATmega328p version?
It turns out the similarity to the 2.56V was a coincidence and probably an error in the datasheet (or in my understanding).
The problem was that after the analogReference(INTERNAL) call the ADC value has to be read immediately! Not after some milliseconds as I did it. (Source)
Still it is important to also wait some milliseconds after doing the dummy readout. For me one readout and delay(5) was just enough, but I guess that depends on the charge left in the capacitor of the ADC: So I'd recommend higher delays.
The correct sequence is:
analogReference(INTERNAL); // set the ADC reference to 1.1V
burn8Readings(A2); // make 8 readings but don't use them
delay(10); // idle some time
nResult1 = analogRead(A2); // read actual value
and
analogReference(DEFAULT); // set the ADC reference back to internal
burn8Readings(A2); // make 8 readings but don't use them
delay(10); // idle again
nResult2 = analogRead(A2); // read actual value
Changing the reference back to DEFAULT seems to be less prone...but at least one readout still was necessary for precise results.
I hope no one has to spend time on this one anymore...

Speed measurement with arduino and ultrasonic hc-sr04 sensor?

i want made speed detection "device" using with Arduino and two ultrasonic hc-sr04 like this link. but I want to make it with ultrasonic instead by LDR.
from that link. how lasers and ldr work, like this
The resistors are used as pull-down resistors and I wired the sensors and put them in a case, to avoid them detecting surrounding light. For each case, a hole was drilled so that the laser beam can light the sensor while the ambient light does not affect the sensor.
The working principle is easy: an object that passes by will "cut" the laser beams, this means the LDR sensor will detect this sudden drop of light intensity. First I defined a threshold value under which the sensor is considered triggered, once the value is under threshold for the first sensor then Arduino waits for the second one to be triggered. During this waiting time it counts the elapsed time between the two events. When the second beam is interrupted, the timer stops and now is just simple math. The distance between the 2 sensors is known, the time between the two events is known, and speed can be computed as speed = distance/time.
Below the Arduino code:
/*
by Claudiu Cristian
*/
unsigned long time1;
int photocellPin_1 = 0; // 1st sensor is connected to a0
int photocellReading_1; // the analog reading from the analog port
int photocellPin_2 = 1; // 2nd sensor is connected to a1
int photocellReading_2; // the analog reading from the analog port
int threshold = 700; //value below sensors are trigerd
float Speed; // declaration of Speed variable
float timing;
unsigned long int calcTimeout = 0; // initialisation of timeout variable
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
photocellReading_1 = analogRead(photocellPin_1); //read out values for sensor 1
photocellReading_2 = analogRead(photocellPin_2); //read out values for sensor 2
// if reading of first sensor is smaller than threshold starts time count and moves to calculation function
if (photocellReading_1 < threshold) {
time1 = millis();
startCalculation();
}
}
// calculation function
void startCalculation() {
calcTimeout = millis(); // asign time to timeout variable
//we wait for trigger of sensor 2 to start calculation - otherwise timeout
while (!(photocellReading_2 < threshold)) {
photocellReading_2 = analogRead(photocellPin_2);
if (millis() - calcTimeout > 5000) return;
}
timing = ((float) millis() - (float) time1) / 1000.0; //computes time in seconds
Speed = 0.115 / timing; //speed in m/s given a separation distance of 11.5 cm
delay(100);
Serial.print(Speed);
Serial.print("\n");
}
how to implement the code with ultrasonic HC-SR04 sensors?
the coding is problem for me. hopefully someone can help me...... :(
Please excuse my poor English !
There are already lots of examples on the internet, so if all you want to do is copy, google arduino sr04
But if you want to know how to do it...
The sr04 has 4 pins, vin, gnd, trigger, and echo.
Connect vin and ground to +5 and gnd
Connect trigger to a digital output pin
Connect echo to a digital input pin
Trigger by going low for 2 microseconds (us) and then high for 10 us then low again
Then get the results with a pulseIn from the echo pin
Read the data sheet for more information

Using arduino analog inputs

I am creating my first Arduino program on the UNO r3. I have played with the Arduino Uno before just with petty example programs, etc. I am using two analog inputs to sense distance using 2 laser sensors with 0-5vdc scaling. These two inputs are 0-5vdc and I have ensured common grounding throughout. The two sensors are named left and right and are input to A0 and A1 respectively. I also have a differential POT which uses a 10K ohm POT wiper voltage as an input on A2. The theory of the program is to take the absolute value of the difference in input voltages between the left and right lasers then determine if the result is greater than or equal to the voltage on pin A2 from the POT wiper. Based on the resulting math, turn on or off a relay interposed to pin D13 via a transistor driver circuit.
The PROBLEM: I cannot achieve accurate changes in voltage on the scale (0-1023) on pins A0, A1, or A2. I have utilized the serial monitor to diagnose this problem. Not sure what the problem is, any help would be great. Also, I cannot achieve a 0 value on any of the above analog pins, even the POT wiper!!!
Here's my code:
const int lf_dist = A0; //names A0
const int rt_dist = A1; //names A1
const int differential = A2; //names A2
const int relay = 13; // select the pin for the relay coil
unsigned int left = 0; // variable to store the value coming from the left sensor
unsigned int right = 0; // variable to store the value coming from the right sensor
unsigned int diff = 0; // variable to store the value coming from the differential POT for maximum distance differential
unsigned int offset = 0; // variable that stores the value between the two laser sensors
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(relay, OUTPUT); // declare the relay pin as an OUTPUT:
analogReference(DEFAULT);
}
void loop()
{
unsigned int left = 0; // variable to store the value coming from the left sensor
unsigned int right = 0; // variable to store the value coming from the right sensor
unsigned int diff = 0; // variable to store the value coming from the differential POT for maximum distance differential
unsigned int offset = 0; // variable that stores the value between the two laser sensors
left = analogRead(A0); // read the value from the left laser
delay(5);
right = analogRead(A1); // read the value from the right sensor
delay(5);
diff = analogRead(A2); // read the value from the differential POT
delay(5);
offset = abs(left - right);
if(offset >= diff) // does math to check if left and right distances are greater than the value clocked in by the differential POT
{
digitalWrite(relay, LOW); // turns off the relay, opens the stop circuit, and turns on the yellow light
}
else
{
digitalWrite(relay, HIGH); // turns on the relay if all is good, and that keeps the machine running
}
Serial.print("\n left = " );
Serial.print(left);
Serial.print("\n right = " );
Serial.print(right);
Serial.print("\n differential = " );
Serial.print(diff);
delay(1000);
}
afaict, this should really be due to the floating pins surrounding the measuring pins, having erratic values, hence perturbating your measures. You should look at your values using arduinoscope, which will show you the interfering effects of the other floating pins on your measuring pins.
The easy workaround for this is to ground all analogical input pins you're not using, and put as much space as you can between both your inputs, so they don't interfere with each other.
I realize this thread is somewhat old not, but perhaps this will help someone. If you power the Arduino with only 5V, as you say you did with a regulator, you will get very erratic behavior, particularly from the analog pins. This is because you will start to brown out the internal voltage regulators that provide the AREF, 3.3, and 5.0 outputs. I've tested this for a robotics project I'm working on, and right around 6.5 volts, everything begins to go wrong. I suppose if you always provided 5.0 input voltage you could compensate for this effect, but in my case I used a LiPo battery that could range from 8.4 volts down to 6.0 volts, and everything goes crazy at 6.5 volts.
The minimum current that arduino sinks in during the sampling from potentiometer should not disturb the actual open input volts at the wiper.
Initialize the pins in pull up mode to avoid garbage values or 'floating' pins or use your own pull down/up resistors at the pins :)

Resources