Arduino adc wrong random values - arduino

i am using arduino uno , atmega328.
on analogRead on the analog pin A0 returns 0 when it is grounded, and with HIGH signal from another pin it returns some value around 1000. But when i connect it to a sensor which gives out around 26 mV it returns random values ...
YYY
My code is very basic, uses analogRead and display it in serial monitor.

A signal with fluctuations of 10mV is not random. The flucatuations are called noise. The standard approach is to add a low pass filter in hardware or software. Since you have a controller anyway I would go for software based low pass filtering. A simple and computational cheap but effective low pass filter is an exponential filter, also known as exponential moving average.

Related

How to get correct values from Arduino sensors

I am using 6 different sensors which are working simultaneously, but I got a problem in the readings from LM35 temperature sensor and MQ-7 Carbon Monoxide sensor. The values are changing depending on the power source.
How can I fix this problem? I'm planning to use a Sony power bank to feed my system but I am getting the wrong values especially from the temperature sensor.
Make sure that your power source is stable and continuous (i.e. make sure that your power bank is working fine and check its current output if its compatible with Arduino)
Make sure that your sensors are wired correct and working fine, try with different LM35 and MQ-7 sensors, if you have that chance.
Make sure that you are supplying your circuit with correct voltage and current, sensors may work unstable if you are supplying them directly from Arduino pins (because maximum current limit of the pins onboard may not be enough for your circuit), try an external power source such as MB102 power module.
Although, it's not a coding problem my suggestion is you can use signal conditioning to solved this. Search about low pass filter and high pass filter to filtering frequency in output sensor.
The simple method is using voltage divider, like this:
Vout = (R1/R1+R2) * Vin
R1 is your sensor, and R2 is resistor.

How to read multiple Analog sensor

I am using Arduino UNO board. I have 24 analog channel which gives me 0~5v analog out put. Now my problem is I have only 5 analog channel. I wanted to read value from each channel for every 2 min and then switch to other channel. Can anyone suggest me in Hardware how can get analog value ?
I am planning to use 8:1 multiplexer or 16:1 multiplexer . Will it is correct way of doing it. Can you suggest other way of doing it in hardware ?
74HC4051,74HCT4051,ADG708,MD14051B,
IC I am planning to Use.dep[end on so,s1,s2 just switch the channel
As a start, you might need to know that even Arduino Uno also have internal MUX. In my experience of reading multiple analog channel, this is the approach that I take. However by taking this approach, I suggest you to recheck the analog value so adding MUX will not generate any error or bias.
This could be done by comparing the output of measurement with the MUX and output of measurement without the MUX. I used 74HC4051 and it works brilliantly, just make sure not to leave any pin floating. The only disadvantage of this method is that you will need to use some I/O to control the MUX, but if that is not an issue for you, then go ahead.
Any other method could be more complicated. It would require your analog channels to correlate with each other, and you need to find a way method to combine multiple analog channel into a single channel.
e.g: if your aim is to compare two analog value, instead of measuring the value and comparing the value in software, you could make use of op-amp comparison circuit to compare the value for you and take the comparison result instead.
Use the photon-pixel coupling method, it is a new approach in science for sampling an unlimited number of sensors in parallel.
Basically, each sensor output is an LED. If you have 10000 sensors, the output of all of them is inserted in a LED array, a LED matrix as the authors say. After that, the LED array is filmed by a video camera and the images are processed in real time by a computer. A software reads one pixel from each LED from the LED array and converts it to numerical values. So, your LED array will be converted in a matrix (with 10000 elements) filled with numbers that can be processed as you wish in your software. I don't know if I was explicit but you can read their article here: https://www.sciencedirect.com/science/article/pii/S2215016119300901
Note that classic multiplexing is serial, this approach is parallel.
The photon-pixel coupling method is truly ingenious because it solves two main problems in engineering: an unlimited number of sensors and their parallel sampling at video rate frequencies. Just imagine, we can read as many sensors as we wish. What I wander is if we can adapt the photon-pixel coupling to Arduino. I am new in the world of microcontrollers but I know Arduino can support a cam, so it should be possible.
If you are a PhD student then:
P.A. Gagniuc, C. Ionescu-Tirgoviste, R.G. Serban, E. Gagniuc. Photon-pixel coupling: A method for parallel acquisition of electrical signals in scientific investigations. MethodsX, 6:968-979, 2019.
To read more analog channels than inputs you have, an analog multiplexer is a good option. All the ones you suggested will work, but personally, I like the Analog Devices ICs for analog circuits, so I would take the ADG708, but this is just a personal preference.

Interrupt works too fast on the Arduino

I know this sounds a bit funny :). But I am trying to eliminate possibilities:
On the Arduino Uno I have attached an interrupt triggered on HIGH to a routine which only increments a volatile defined long counter. This counter is displayed on an LCD screen.
If I connect a pulse generator with a frequency of 1 Hz at TTL levels, I would expect the counter to increase with about 1 per second. However this is not the case.
As the frequency is 1 Hz (duty cycle 50%) could it be possible that once the counter is incremented the IRS is exited (and clears the interrupt flag) BUT: the INT0 level is still HIGH so the ISR would be called again? At 1 Hz 50% duty, the HIGH would stay for 500 ms and at 16 mHz...
The processor at the heart of any Arduino has two different kinds of interrupts: “external”, and “pin change”. There are only two external interrupt pins on the ATmega168/328 (ie, in the Arduino Uno/Nano/Duemilanove), INT0 and INT1, and they are mapped to Arduino pins 2 and 3. These interrupts can be set to trigger on RISING or FALLING signal edges, or on low level. The triggers are interpreted by hardware, and the interrupt is very fast. The Arduino Mega has a few more external interrupt pins available.
So as commented: It triggers on an edge!
See more details on the Arduino Playground web page.
Two electrical reasons can explain why interrupt does not function as you need.
1- The pulse generator output and MCU input can have an impedance mismatch, which can cause ringing on the waveform edges. For example, if your function generator has a 50 ohm output capable of generating high frequencies you might see a problem driving a high impedance input like the Arduino at low frequency.
The name "pulse generator" makes me think this is a 50 ohm out device intended to make very short pulses with sharp edges. In such a case, you add a terminating resistor at the destination (load) to match the impedance of the source (pulse generator). For a 50 ohm output, 47 ohm would be close enough. If the output is 100 kohm, then place a matching resistor at the Arduino.
2- Just the opposite, the generator waveform edges may be so slow that the voltage passes through TTL 0 to 1 transition multiple times. If you have noise on your signal input, a slow edge could be causing multiple triggers. For example, if you are picking up some 60 Hz ripple from a power supply and grounding issues, your square wave edges won't be as square as you think.
In such cases hysteresis is a solution. There are many ways to de-glitch (debounce) in code. There is no answer that is right for all problems. A simple example would be that the ISR you require that the input reads high twice in a row for the edge to be accepted.

Analog readings on Arduino returns wrong values

I have tried measuring the analog pin value using the provided standard sketch in the Arduino IDE. However even when there is no connection to the pin, it prints out random values. Is there anything that has to be taken care of?
I have a FSR sensor connected from 5V+ to analog pin 0
No connection giving you random value is completely expected. The pin is floating, and may or may not have a charge on it at any time, giving random values.
Attach the analog input to ground - it should return a number approaching zero (within the accuracy and noise of the ADC).
You have to use "Pull-up" or "Pull-down" resistor. Read here about this circuit: http://www.ladyada.net/learn/arduino/lesson5.html
Providing a path of higher resistance to ground ensures that when that portion of the circuit is open, the static electricity remaining in the portion connected to the pin gets grounded. It also splits the voltage between both paths to allow you to properly throttle the voltage to the pin.
The same problem arise to me ,
What you have to do is change the" Serial.begin(9600);"
To "Serial.beigin(115200);
Now it is showing exact zeroes without any input source.

Arduino and External Peripherals

I am a CS guy getting started with Arduino. This is probably a very basic electronics question but from going over the arduino tutorials everything is connected to the arduino with a resistor.
Well since i am following the tutorials i know what type of resistor i should use but what i do not know is why i should use one? and What type of resistor to pick i am to do something which is not covered in a tutorial.
The resistor simply serves to limit the current into or out of a pin in case something goes awry. If your AVR decides to output high on a pin that something else wants low (or vice-versa), large, damaging currents can occur if not limited by some resistance. The current limit for AVRs is about 20 milliamps, and given that the voltages are usually 5V, something larger than 250 ohms "would work".
To give a margin of safety, 1-10k is a great choice; for digital signals it seldom matters unless you're into very high-speed applications (beyond the AVRs capability anyways). For analog inputs, a similar resistor would also be advisable, as the amount of current the ADC takes to sample is negligible when your resistor is in the few kilo-ohm range.
The underlying principle that you want to learn is Ohm's Law, which describes the relationship between voltage, resistance, and current in a circuit.
Resistors are used to
limit current,
devide voltage
protect against over voltage
pull-up, pull down
current to voltage conversion
etc ...
1) limit output current, the absolute max current per IO is 40mA, a typical LED works on ±2V 20mA.
the resistance value can by calculated by (5V - 2V)/(0.02A)=150Ω usually a 220Ω resistor is used, because: it consumes less power, there doesn't flow 20 milliamps, and there is no notable difference in emitted light.
2) if you have a analog voltage that variates between 0 and 10 Volts, you 'll need a voltage divider of 1/2. pick by example z2 10k and calculate z1 by 10k*(Vin,max/5V -1). take a value of resistance higher than the original calculated. and recalculate the new Vout.
3) place a resistor of 10k in series between the analog input of the arduino and the 'to measure voltage'
3) if you have to measure a analog current, you place a resistor to ground and the analog input, calculate the resistor by Z=5V/amps.
4) if you connect a button to the arduino, you 'll need to place a pull up or a pull down resistor. if you 're not using a resistor, the input is floating and can take any value (high or low). or you can enable the internal weak pull up. by pinMode(xx,INPUT); digitalWrite(xx,HIGH);. and disabeling by digitalWrite(xx,LOW); by default the pull-up is disabled.

Resources