Cheapskate wants to DIY an optical distance sensor for 4-20mA current loops - Page 1
Forum section: electronics / beginners

author: wobbly
author_rank: Regular Contributor
timestamp: August 19, 2025, 01:48:22 pm
content: Hi all, beginner hereI'm currently learning about industrial control using PLCs and I'd like to build a circuit that can convert a sensor's output of 0-5V into a current output compatible with standard industrial 24V, 4-20mA current sensor systems.I have a Sharp "0A41SK F" sensor (datasheet).The PLC I'm thinking of using is an Automation Direct "C0-04AD-1"(datasheet)which supports 2, 3 or 4-wire current loop transmitters.I'm not very familiar with analog electronics so please bear with me.My thought was to build some circuitry around the sensor that would work with a 4-wire current loop, that way the 5V sensor supply can be taken from the 24V supply rail by using a simple linear regulator (7805 or LM317 perhaps). I'm thinking that's probably simpler than trying to run from a 2-wire system.But I'm stuck on the problem of converting the 0-5V sensor output to a scaled current output onto the 24V signal wires. The PLC has a current sinking type input.My first thought was to use an NPN transistor (gain circa 100x) between the GND and 24V current loop wires, with the output from the sensor driving the base through a 22K resistor. At 5V this would cause the NPN to produce 20mA, which is good, but it obviously doesn't work when the sensor outputs 0V - because the NPN would fall to 0mA.So how can I redesign this so that 0-5V scales to 4-20mA using simple circuitry?Disclaimer: My PLC is one I've bought solely for the purposes of learning about them, none of this hardware will ever be used in a real-world system and there are no dangers involved if this sensor fails in use.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 19, 2025, 04:25:11 pm
content: It would be tough because of your choice of sensor. It doesn't seem to output 0-5V and the output doesn't seem linear to the distance. If the output is 0 to 5V and is linear to the distance you can put a 126 Ohms in series with the sensor. The input has 124Ohm so the 0-5V will generate 0 to 20mA.

author: wobbly
author_rank: Regular Contributor
timestamp: August 19, 2025, 04:30:00 pm
content: I don't mind the nonlinearity or the fact it tops out at 3-ish volts. I can deal with that on the PLC.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 19, 2025, 04:32:14 pm
content: You will lose the far range where the output drops below 4mA. Or you can pick the right sensor. By the way which CPU you use with the input module? Unless you have the module for free it's best to pick the input to match the output of the sensor. The prices are the same.

author: pqass
author_rank: Super Contributor
timestamp: August 19, 2025, 05:01:47 pm
content: QuoteI don't think it will be simple. See thissimulationand attached schematic.And hFEis temperature dependent.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 19, 2025, 05:41:09 pm
content: Checking the specs on your module and it's a 0-20mA module so you can read the entire range of the sensor. Simply connect a 50Ohms resistor between the V0 of the sensor and the CH1 input. The GND of the sensor connects to the 0V of the 24V power supply (for the module), 5V power supply for the sensor and the 0V terminal of the input module. Connect the 24V from power supply to the 24V terminal of the input module. Connect the 5V to the Vcc of the sensor. Now you will have the current from about 1.5mA to 18mA and you can scale that in the PLC input set up. Then I think the sensor response is pretty much linear to the inverse square of the distance. The Click PLC although cheap has good math function so it would be relatively easy..

author: wobbly
author_rank: Regular Contributor
timestamp: August 19, 2025, 05:51:28 pm
content: Quote from: BeBuLamar on August 19, 2025, 04:32:14 pmI'm not wedded to this particular sensor really, I was just after a method to convert and scale the output. I'm told that in the industry the most common form of sensors use the 4-20mA interface so I want to spend my minimal budget on things that are as close to real-world as possible, which in this case is the "C0-04AD-1". I just hoped that if there's a quick way to do the scaling then it might open the door to me building a set of different sensors on the cheap.PLCs are not a good hobby for the pennilessThanks all, I'll try something else.

author: tszaboo
author_rank: Super Contributor
timestamp: August 19, 2025, 05:56:05 pm
content: You place a 250 ohm resistor below the transistor. Feed back the voltage from that into an opamp. Opamp + is the sensor, - is the feedback. Opamp output goes to base of transistor.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 19, 2025, 06:05:21 pm
content: Quote from: wobbly on August 19, 2025, 05:51:28 pm

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 19, 2025, 06:08:43 pm
content: If you just connect them as I suggested I am sure it will work. I work with PLC so I do have a number of them at home without having to pay and yes they are expensive. I actually often use 0-10V rather than 4-20mA unless when long cable is needed.

author: mikerj
author_rank: Super Contributor
timestamp: August 20, 2025, 08:59:18 am
content: Quote from: BeBuLamar on August 19, 2025, 04:25:11 pmNo, this will not work. You can pass the output of a 4-20mA sensor into a resistor to give 0-5v or 0-10v for a PLC, but you can't do this the other way around. In a 4-20mA system the sensor has to sink current from the PLC, and the open loop voltage will typically be 24v. To do this you need an active current sink circuit which can be created with an op-amp (many designs on the internet and a search on here will find some) or you can use a dedicated IC like the XTR115/XTR116.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 20, 2025, 09:14:52 am
content: Quote from: mikerj on August 20, 2025, 08:59:18 amNo on the OP practicular input module. The power comes from the sensor the input doesn't have power.

author: tszaboo
author_rank: Super Contributor
timestamp: August 20, 2025, 09:23:10 am
content: Actually, what I wrote wouldn't work all the time. Your sensor has 0-5V output, not 1-5V.So an offset needs to be placed in the system.Some idea on how to do this. You might need to play a bit with the resistor values, this only goes down to 5mA.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 20, 2025, 10:26:25 am
content: Quote from: tszaboo on August 20, 2025, 09:23:10 amOnce again the input module the OP has is 0-20mA input and not 4-20mA so there is no need for the offset.

author: Zero999
author_rank: Super Contributor
timestamp: August 20, 2025, 11:31:00 am
content: A Howland current source will do.This circuit scales 0 to 3.2V to 0 to 20mA.V2 is the output of the proximity sensor.The PLC input is modeled by the 124R resistor.

author: wobbly
author_rank: Regular Contributor
timestamp: August 20, 2025, 12:01:49 pm
content: Thank you all for the ideas, I'll try them out over the next few days.

author: Zero999
author_rank: Super Contributor
timestamp: August 20, 2025, 01:35:44 pm
content: Note with the above schematic, the proximity sensor can be powered by the LM78L05.How accurate does it need to be? If it's just approximate some temperature drift isn't an issue, a floating supply for the proximity sensor is acceptable and an inverting output is okay, it can be simplified.This circuit gives about 20mA to 6mA, when the output of the sensor is swept from 0.1V to 3.1V. It's not very accurate because the current is dependent on VBEwhich is temperature sensitive.V2 models the LM79L05. The LM79L05's output goes to the 0V of the proximity sensor, the LM79L05's ground to V1's + and the LM79L05's input to circuit ground. Yes, this is confusing. It works because all voltages are relative.

author: Terry Bites
author_rank: Super Contributor
timestamp: August 20, 2025, 04:55:11 pm
content: https://www.aliexpress.com/item/32829055550.html?spm=a2g0o.productlist.relatedlink.3.356a4201ypxllG

author: wobbly
author_rank: Regular Contributor
timestamp: August 21, 2025, 05:20:14 pm
content: A couple of days and a rethink later...The Click PLC I'm using has a serial port on it. It allows you to send RS-232 signals and receive them from within the ladder logic program. I figured I can build a microcontroller board and program it to masquerade as a piece of factory equipment such as a water tank for instance.The Micro would simulate having things like a water-level sensor, thermistors, a heater and drain and fill valves. The imaginary water-level and thermistor 'ADC' readings can simply be sent over the serial port and they would appear in the right memory locations ready for the ladder logic to process them. Stimulating the PLC's digital inputs can be done with optocouplers, and the PLC's outputs can simply be hooked up to pull the micro's input pins to GND.That way I can simply simulate almost any piece of industrial gear (within the limits of my PLC's available IO pins) by reprogramming the micro!And all for £0.00!Picture below shows the PLC reading 3 bytes from the serial port. Haven't tried this yet but I think it'll look something vaguely like that. One such message for each different ADC reading, stored in dedicated memory locations.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 21, 2025, 06:17:50 pm
content: What do you mean by zero money? You have to buy a microprocessor, the sensor, etc.. The PLC is designed to easily interface with different kind of sensors. By the way which model of the Click CPU do you have?

author: wobbly
author_rank: Regular Contributor
timestamp: August 21, 2025, 06:41:55 pm
content: There is no sensor any more, that's the point. And I can pull the micro out of some old broken gearThe CPU is a C0-00DR-D.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 21, 2025, 07:44:27 pm
content: If there is no sensor then you actually do not measure any distance right?

author: mikerj
author_rank: Super Contributor
timestamp: August 21, 2025, 10:45:48 pm
content: Quote from: BeBuLamar on August 20, 2025, 09:14:52 amThe sensor has a 0-3v output, you cannot turn this into a 4-20mA or 0-20mA signal for a PLC etc. with just a resistor. It is just not possible.

author: mikerj
author_rank: Super Contributor
timestamp: August 21, 2025, 10:48:21 pm
content: Quote from: wobbly on August 21, 2025, 05:20:14 pmDoes your PLC support Modbus RTU? There are open source librarys available to implement a Modbus RTU slave on Arduinos or other micros.

author: Zero999
author_rank: Super Contributor
timestamp: August 22, 2025, 07:24:32 am
content: Quote from: wobbly on August 21, 2025, 05:20:14 pmWhat does any of that have to do with the original question?If you've changed your mind about what you're asking but want to keep to the same thread for context, then please be clear about it. Ideally append the original power with EDIT: "This is what I want to do now." for clarity. And please don't delete or radically change anything. Usestrikethroughif you must, because then it's clear what you originally wanted.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 22, 2025, 10:13:26 am
content: Quote from: mikerj on August 21, 2025, 10:45:48 pmYes I can. I don't have a 0-3V sensor but I do have 0-10V sensor and 0-20mA input and I can do it.

author: wobbly
author_rank: Regular Contributor
timestamp: August 22, 2025, 10:51:26 am
content: @Zero999Sorry, you're quite right. I'll start a new thread.

author: BeBuLamar
author_rank: Super Contributor
timestamp: August 22, 2025, 11:18:28 am
content: Quote from: mikerj on August 21, 2025, 10:48:21 pmIt does support Modbus RTU Master or Slave.