Hi everyone,
First, I'd like to thank Ben for his tutorial on modbus TCP/IP with logic IO, I've successfully replicated it on my Arduino MEGA 2560 + W5100 Ethernet Shield R3 with the Mudbus library from Martin.
Now, I'm working with an HC-SR04 ultrasonic sensor.
For those who are not familiar with this device, let me explain it with more details:
It sends and receives ultrasonic waves to measure distance (just like bats)
It has four pins:
-VCC and GND: for powering the device (the 5V from any Arduino is enough)
-Trig: connected to a digital pin on the Arduino, when it receives a digital output signal from the Arduino, the transmitter sends an ultrasonic pulse.
-Echo: connected to a digital pin on the Arduino, when the ultrasonic pulse bounced back from an object and made it into the receiver, the signal becomes a digital input to the Arduino, and the measured distance could be set up to be shown on the Arduino IDE serial monitor.
For further details, there's some code to do it with Arduino IDE:
/*
* created by Rui Santos,
randomnerdtutorials.com
*
* Complete Guide for Ultrasonic Sensor HC-SR04
*
Ultrasonic sensor Pins:
VCC: +5VDC
Trig : Trigger (INPUT) - Pin11
Echo: Echo (OUTPUT) - Pin 12
GND: GND
*/
int trigPin = 11;
int echoPin = 12;
long duration, cm, inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = (duration/2) / 29.1;
inches = (duration/2) / 74;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);
}
And an alternative code with a library called "NewPing":
/*
* Posted on
randomnerdtutorials.com
* created by
playground.arduino.cc/Code/NewPing
*/
#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and
maximum distance.
void setup() {
Serial.begin(9600);
}
void loop() {
delay(50);
unsigned int uS = sonar.ping_cm();
Serial.print(uS);
Serial.println(‘‘cm’’);
}
Now, the problem I'm facing:
1-The Echo pin has to be configured as a digital input and has to be connected to a digital pin on the Arduino, and by using the "pulseIn" function in the Arduino IDE, the resulting measured distances are integer values.
2-I want to display such distances in the form of a bar or a trend on Ge on Proview (which require analog values). If I'm not mistaken, digital-type channels are compatible with digital values (0 or 1). If I use analog-type channels, I'm afraid it would not be able to read values (the Echo pin of the ultrasonic sensor has to be connected to a digital pin on the Arduino), and I'm not sure if integer-type channels could solve it.
And finally, my questions:
1-Which type of channel should I use on my modbus module on Proview?
2-I'd like to know more about how to use integer-type channels. With which type of physical IO interface could they be used: analog or digital, or maybe both?
Any help appreciated
Best regards
/Maverick