Arduino Mega, analog in/out questions

Allright, people here convinced me to buy an Arduino (Mega) board and I'm playing with the software now. I must say the price is good and the software + examples I've seen are really user-friendly! I did a PICF18 years ago and well... what a maze.

Anyway, I have some questions about analog in/output. I have to make a module that measures the peak pressure between 2 pulses that should generate an interrupt. When the second pulse occurs, I have to copy the analog input(pressure) to the output. This will be sent to another PLC (which is too slow too quickly read pressure, that's why). - Reading analog input ASAP... I could do it in the mainloop, or is it also possible to create a timed interrupt? If possible, I'd like to read 2 analog input values at least 2000 times a second.

  • Writing analog output. I saw the "analogWrite" only goes from 0 to 255. Not a problem in our case, but is the accuresy really that limited?

  • How long does it physically take for an output to stabilize at the given value? Right after setting the output, I need to send a "ready" pulse to the other PLC which will immidiatly read the given output. Should I create a small delay:

analogWrite( pressureOutput1 ); // Send to the PLC // wait... but not in the interrupt digitalWrite( readyPin1 ); // tell PLC the analog output can be read

Or is it so fast that it isn't nescesary?

  • Last question (for now :) ) The peak value will be shared amongst 3 processes: interrupt_startReading --> peak = 0 interrupt_sendReading --> analogWrite( x, peak / 4 ); mainLoop / timedInterrupt --> if value > peak then peak = value

The 2 pulse interrupts will never occur at the same time. But the last routine that writes the peak value happens continously. How to make sure I'm not sending half-written values or skipping the reset?

Regards, Rick

  • Writing analog output. I saw the "analogWrite" only goes from 0 to 255. Not a problem in our case, but is the accuresy really that limited?

Wow, lots of questions. I will deal with the quoted one above. The Arduino analogWrite is not a true analog output function (they really should have named it PWMoutput instead) but rather a PWM output with only 255 steps of precision. To turn this into a true analog output voltage then one needs to build an external low pass filter (using passive or active components) and possibly some active buffering and/or amplification to drive load impedances. So unless your PLC can directly handle a 5vdc PWM signal directly you will first have to deal with that issue before going much further in my opinion.


That’s a bummer. The PLC inspects a signal between 0 and 10 volt indeed. Are there arduino boards that support true analog output?

I’m really not into electronics, but is this modification on the board difficult in terms of quality? The point is that the PLC needs to read a stabile analog signal within 5 milliseconds after I had a certain pulse. Any examples for doing this properly?

Thanks for your reply,

Are there arduino boards that support true analog output?

No, none that I’m aware of, especially with your 0-10vdc output requirement. This would have to be mounted on an external board, either stand-alone or a Arduino “shield” type accessory board. What is required is a digital to analog convertor chip that communicates with the Arduino via I2C or serial data and outputs a pure analog DC voltage that can be scaled to 0-10vdc output.

There probably is such a commercial product available but industrial type analog modules tend to be on the pricey side.

Perhaps someone else here can recommend a product that would work for you?


0..5 volt is ok as well. I thought analog outputs were a very common thing on all these boards... but maybe that's one of the reasons the prices are so low compared to other controllers such as PLC's.

A Digital/Analog chip could do the job, but isn't serial communication slow? Speed is a key factor here as the PLC might need to read about 70 samples per second. You also mentioned I2C... I see it everywhere on microcontroller related sites, but what exactly is it?

One alternative route is to calculate a complete array of pressure values and send it via the serial cable once per second. Output through hardware would be a nicer solution for this situation though.

Thanks, Rick

I thought analog outputs were a very common thing on all these boards

The underlining microprocessors, AVR and PIC series are the most popular with hobbyist, all tend to have analog voltage input features, but only a few higher end types have true analog D/A outputs, and none of the ones that the Arduino platform support have built in D/A hardware features.

I2C communications is a two wire, high speed communications link designed to connect complex IC devices together over short distances. The Arduino system supports this interface standard and there are zillions of devices that one can hook up.

Here is an example of a higher end A/D convertor that uses I2C.

Here is an example of a nice D/A convertor that might be part of your solution:


That clarifies. And thanks for the links. If I understand it right, I2C is like the CANbus for microcontrollers on short distances. It's all new for me so excuse me if I sound dumb.

I'll have to check if the PLC can swallow the PWM signal, but I think not. Besides D/A convertors, are there also PWM-to-Analog convertors... like you were explaining in the first answer?

Probably I'll go for the array of readings through the serialport instead of using hardware output, but if there is a real simple and compact way to convert the PWM output to something usefull for the PLC, it's worth checking.


are there also PWM-to-Analog convertors

You know I don't think so. As the design of the low pass filter would have to be specific to the PWM operating frequency (there is no such thing as a 'standard' PWM switching frequency) it would take a pretty complex PWM/analog convertor chip that would have programmable filtering, etc.

I know that some people have written modbus communications software functions to run on boards (like an Arduino) that can read/write directly to a PLC that has serial data ports (most do). I read that you didn't think that would be fast enough, but that would depend of the baud rate that the PLC can handle and you would be suprized at how much data (variables) one can push at the higher baud rates.


Lefty suggested “Perhaps someone else here can recommend a product that would work for you?”

Whilst those D/A products listed could be used, a cheaper option would be a simple voltage divider using an 8-bit: 256 Resistors (257 Steps) Digital Potentiometer.

The OP said 0-5V is ok, so consider something like a MCP41X1 for < $1, shown on 1st page of datasheet

And here’s a similar example

NB: this suggestion assumes your PLC has very high input impedance.
ie. much > the impedances used in the voltage divider.

I'll have to pass these links to someone with electronic knowledge at my work. The only thing I can do is inserting a battery in a clock and putting my fingers in a fusebox. I'm going to purchase a starter kit to hobby at home and hopefully learn something about the electric side as well. But this project is for work, so I can't play around too much.

My concern with additional components is quality drops or delay will be introduced (especially if you don't exactly know what you are doing, like me). But maybe I'm completely wrong. I think its certainly worth a try, but I'm also going to write passing values via the serial port as an alternative.

I've checked the baudrate for my PLC (a OMRON CP1L or CP1H), it should support 38400. Let me see, that would mean I can send about 2400 16bit values per second right? Should be more than enough, but how accurate is it? My interrupts will have to send a 16 bit value towards the PLC about 140 times per second. In the worst case both interrupts will trigger at the same time. I must make sure that all samples are received, and in the right order.

I can also list the samples and send once per second, easy going. But in that case the microcontroller also has to perform other tasks such as stopping the motor immidiatly when reading a wrong sample. That is certainly possible, but the PLC and Microcontroller program will start overlap each other with tasks, making the program as a whole more difficult to understand (for other maintenance people).

Everyone thanks alot!

Apologies for bumping an old thead, tried using the search feature before posting a new question. Figured this was closest to my question ;)

When using analogwrite() on the digital pins, does that mean any external device we want to hook it up to must be expecting a digital input? I was hoping it would be possible so that I can hook up the Arduino to one of the analog inputs on an external device. I was also planning to change the 8-bit resolution to 10-bits (Searched on how to change it).

I was initially going to go with Crossbow's motes, but they don't have any analog outputs. A friend mentioned to check out Arduino. This board is quite functional! Looks like a good starting point.

When using analogwrite() on the digital pins, does that mean any external device we want to hook it up to must be expecting a digital input?

Yes, unless there is external low pass filtering applied to the PWM output then a receiving device would need to input it as a digital input and then 'decode' the PWM information in software to come up with the final variable. Kind of ackward and time consuming.