Have some basic questions on how analog sensors work.

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
I recently built my first Arduino project and I feel like a lot of things are starting to click in place in regards to my understanding of electronics, but I just want to make sure I've got things straight. The more I do stuff with this the more I feel like the Arduino can be thought of as a multimeter with a programmable processor attached.

An analog sensor produces a continuously variable physical quantity, such as voltage. Analog sensors output some kind of analog signal to an analog input pin on an Arduino.

- A photoresistor receives an input voltage from the Arduino and produces a resistance that varies with the light intensity. The Arduino reads this resistance and in software can map it to a light intensity.

- A microphone (a "sensor" for sound waves) outputs a continuously-varying voltage in the millivolt range and an A/D converter can read this voltage (ideally at a high sampling rate like 48,000 times a second) and convert it to digital.

- Uhhh.. are there sensors that work by outputting a certain current that the Arduino will read between two pins?

Is that it? Is that how physical sensors interface with the digital world? Just voltage or resistance readings that get mapped to digital values? Or a combination of the two at the same time?
 

ctbaars

Golden Member
Nov 4, 2009
1,565
160
106
Generally. Yes. A/D converters convert voltage to digital. If something puts out current as a measurement form, a sense resistor is used to convert to voltage. I've used many sensors and always comes down in the end as ... voltage.
 

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
Generally. Yes. A/D converters convert voltage to digital. If something puts out current as a measurement form, a sense resistor is used to convert to voltage. I've used many sensors and always comes down in the end as ... voltage.

What about in the case of a photo resistor? The resistance changes with light intensity, so does the A/D converter output a known voltage and current, and measure the voltage drop across the photoresistor (in series with another, second, reference resistor) to determine the light intensity?

So basically an Arduino can be simplified even further to "a voltmeter attached to a processor?"
 

ctbaars

Golden Member
Nov 4, 2009
1,565
160
106
A/D converter has a Voltage input. Digital output. Period. IIRC You can get a standalone I/D converter too. But ultimately, they are a standard A/D with a sense resistor and a differential amplifier tacked on on the input.
There are other factors to make circuits work but that's a totally different subject.
 

dullard

Elite Member
May 21, 2001
25,784
4,324
126
An Arduino is just a simple microcontroller with a few added components (which components depends on the specific Arduino board). For example, the Arduino Uno has USB chips added on for USB communication, a few power control chips, and a crystal oscillator to provide proper timing to the microcontroller.

The Arduinos that I've seen all have Atmega brand microcontrollers. A microcontroller is a processor with added components. For example, a microcontroller will likely have RAM and/or ROM, they often have A/D chips, timers, etc.

So, I would personally consider the Arduino to be a processor with attached voltmeter (rather than the reverse).

You do not need to use the Arduino's A/D converter. You can use your own. You can read your sensor with any voltage and any current you want (within reason). Often, to get really low signals for light sensing you might want to use an integrator instead of an A/D converter. Basically an integrator has a voltage that rises over time based upon the signal from your sensor, the higher the signal, the faster the integrator's voltage rises. This lets you measure very tiny signals such as light levels so dim that your eyes cannot see.
 

Red Squirrel

No Lifer
May 24, 2003
69,839
13,409
126
www.anyf.ca
Technically every sensor is analog at some point, it's just that a "digital" sensor has some electronics built in to output a digital signal such as an i2c or spm, or perhaps an analog output that's been "digitized" (cleanup noise etc).

To measure current you need to convert it into a voltage first, as current on it's own is not really something that can be read. You do this by using a shunt resistor and measuring between the two leads. This is tricky to do if the shunt is not on the negative side of the circuit, and in my experience I find I pickup so much noise anyway I can't seem to get a consistent reading. You can get current transducer ICs that will do it for you and then output a voltage range. It's also better to try to stick to high side current measurement so you're not putting any breaks/resistance in your main ground path.

You can also buy separare ADCs (the analog inputs of the arduino are ADCs). So if you need more inputs or are using something like the Raspberry PI that does not have built in ADC you can still add your own. Typically they'll communicate digitally through i2c or something.

I have not played all that much with that stuff myself though but been wanting to.
 

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
On the topic of I2C, how difficult is it compared to basic UART serial?

Having implemented UART serial communication between an Arduino and ESP8266 chip, UART *sucks*. To have good fault tolerance you have to set up polling and handshakes manually in the Arduino code.

- send a special character to the receiving device
- receiving device waits for special character and sends back another special character to confirm receipt
- transmitting device waits until it receives the confirmation character to prove that a connection has been established
- transmitting device sends data with special start and end markers like "< and >" to differentiate the start and end of a message block and then stops to wait for response
- receiving device waits for message and sends a response that it has a complete message once < and > have been received
- transmitting device gets response from receiver and sends the next block of data with the same < and > markers

And you might also have to code things that manage buffer issues and timeouts.

This is a pain in the ass. Does I2C abstract away this stuff to the point where it is more "plug and play?"
 

Red Squirrel

No Lifer
May 24, 2003
69,839
13,409
126
www.anyf.ca
TBH I have not played with I2C myself yet but from my understanding there are libraries and such that will do a lot of the dirty work for you so it's probably easier. Though I was under the impression UART mostly did the same, I wrote a really basic console interface to configure a custom solar controller and it was not too hard to code, like I can plug a laptop via USB and it picks up as a virtual serial port. The actual interface was quite primitive though, like something you'd see on a really old piece of telecom equipment. :p
 

ctbaars

Golden Member
Nov 4, 2009
1,565
160
106
I'm not a programmer:
But my hardware design uses I2C for very local communications. Front Panel display. EEPROM. I use UARTS for all the external communications, (converted to RS232 or 485). And Ethernet which is its own hardware block.
None of our UARTS use that level of handshaking, ever. Our software has an option for an ASCII checksum. I've learned from all my customers, it's never used.
 

Red Squirrel

No Lifer
May 24, 2003
69,839
13,409
126
www.anyf.ca
You can't really measure current directly, any method of measuring it is actually measuring a voltage that is created by the current. One way is using a shunt which is just a precision resistor of very low value. The voltage between both end changes slightly based on how much current is going through it. You then use an op amp to bring it to a voltage level the micro controller can read.

There are other ways too such as hall effect sensors.
 

drinkmorejava

Diamond Member
Jun 24, 2004
3,567
7
81
TBH I have not played with I2C myself yet but from my understanding there are libraries and such that will do a lot of the dirty work for you so it's probably easier. Though I was under the impression UART mostly did the same, I wrote a really basic console interface to configure a custom solar controller and it was not too hard to code, like I can plug a laptop via USB and it picks up as a virtual serial port. The actual interface was quite primitive though, like something you'd see on a really old piece of telecom equipment. :p

I have to second this. UART on the arduino is basically plug and play. If you're okay with the default speed, you pretty much connect the wires and start reading or writing. It is only 5 volts, so you may need a level converter (https://www.sparkfun.com/products/589) depending on what you're interfacing with (ie when building my own arduino from bare components my usb to serial cable would work, but the real serial port on my computer would not because the standard is 13v). I2C on the other hand involves several more steps because you have to tell the device when you want to send and receive data...but the libraries make this straight forward.

One thing to note is if you are using the hardware uart (the normal serial pins), or if you added a library for software uart to use some of the other gpio pins. I've used software UART before and it works, but because arduinos don't multitask, it can get funky.

Another thing to consider, but I don't believe you did this. If you built your own arduino and didn't use an external crystal oscillator, your timings could be getting out of sync.
 
Last edited:

Red Squirrel

No Lifer
May 24, 2003
69,839
13,409
126
www.anyf.ca
The MCP2221 (actually I guess the A version might be better now) chip is pretty nice if you want to add a UART to a bare chip too. I got the process down path now for building Arduino based projects without an actual Arduino. I'll use the Arduino for testing but once I want to make it a project that I keep I just use the bare chip.

I need to take the time to learn Kicad properly though so I can get some PCBs made for future projects, the time and effort I save will be worth the cost, so I don't have to do this again:



That was more tedious and harder than it looks haha.

Don't mind that weird solder blob, that was a makeshift heatsink for a linear regulator. LOL.