Monday, January 26, 2015

Programmable Logic Controller (PLC) - Ultrasonic Sensor - Arduino

Programmable Logic Controller (PLC) application about distance measurement using ultrasonic sensor. Ultrasonic sensor connect to the Arduino, And then Arduino connect to the PLC using serial port and ModBus communication protocol.

Distance measurement application used for example: Liquid / Water Level Sensor non contact (probe), Object Detection Sensor, Object Distance Sensor, Check the amount of products (Products Checker), and etc.

A. Example PLC Application: Products Checker

Products Checker using Ultrasonic Sensor

B. Hardware

Hardware of ultrasonic sensor in PLC application

1. Ultrasonic sensor HC-SR04
2. Arduino Uno
3.TTL to RS232 male
4.RS232 PLC Cable
5.PLC with Modbus Support (Modbus Slave)
6. Resistor
7. Seven Segment Common Anode
8. Adaptor for Arduino

C. Wiring

Wiring of ultrasonic sensor in PLC application

Wiring of Seven Segment in Siemens PLC

For about display seven segment with PLC, check the link:

D. Software
Download Project File:
1. Arduino SimpleModbusMaster Library (Copy-Paste into Folder C:\Program Files (x86)\Arduino\libraries), click here
2. Arduino Project, click here
3.Ladder Programming for Siemens PLC S7-200, click here

E. See The test result:

F. Set the distance ranges in PLC ladder programming

Set the distance ranges in PLC ladder programming

G. Arduino Code
#include SimpleModbusMaster.h
//Arduino Serial Port Connect to Port 0 of Siemens PLC S7 200
#define slaveAddr 1
#define baud 9600
#define timeout 1000
#define polling 200
#define retry_count 0
#define TxEnablePin 2

Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
unsigned int writeRegs[2];

#define Trig 13
#define Echo 12

const float CmFactor = 29.0;
const float InFactor = 73.746;

void setup() {
modbus_construct(packet1, slaveAddr, PRESET_MULTIPLE_REGISTERS, 0, 2, writeRegs);
modbus_configure(&Serial, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);

pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);

void loop() {
long durationMicroseconds, distanceCentimeters,distanceInches;
digitalWrite(Trig, LOW);
digitalWrite(Trig, HIGH);
digitalWrite(Trig, LOW);
durationMicroseconds = pulseIn(Echo, HIGH);
distanceCentimeters = (durationMicroseconds/2) / CmFactor;
distanceInches = (durationMicroseconds/2) / InFactor;

writeRegs[0] = (word) distanceCentimeters;
writeRegs[1] = (word) distanceInches;


Newer Post Older Post Home

You may also like these ebook:

Get Free PLC eBook directly sent to your email,
and email subscription to

We hate SPAM. Your information is never sold or shared with anyone.

Your Email Will Be 100% Secured !