How to output multiple sensor reading to an LCD?

I need some help and am pretty new to using an Arduino.
Below is code I have used to have 2 sensors measuring a distance, And need help in how to output those readings to an LCD in the form of

"Distance Sensor

Distance1 : A
Distance2 : B


#include <NewPing.h>
#include <LiquidCrystal.h>

#define SONAR_NUM 2 // Number or sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

int backLight = 6; // pin 13 will control the backlight

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(43, 42, MAX_DISTANCE), // Each sensor’s trigger pin, echo pin, and max distance to ping.
NewPing(45, 44, MAX_DISTANCE)



void setup() {
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer = pingTimer[i - 1] + PING_INTERVAL;

  • lcd.begin(20, 4);*

  • lcd.print(“Powering On…”);*
    void loop() {

  • pinMode(backLight, OUTPUT);*

  • digitalWrite(backLight, HIGH); // turn backlight on. Replace ‘HIGH’ with ‘LOW’ to turn it off.*

  • for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.*
    _ if (millis() >= pingTimer*) { // Is it this sensor’s time to ping?_
    pingTimer += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
    if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
    sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
    _ currentSensor = i; // Sensor being accessed.
    cm[currentSensor] = 0; // Make distance zero in case there’s no ping echo for this sensor._
    sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    _ }

* lcd.setCursor(0, 0);*
* lcd.print(“Reversing Sensor”);*
//////// code to send data to LCD//////////////////

void echoCheck() { // If ping received, set the sensor distance to array.
* if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
void oneSensorCycle() { // Sensor ping cycle complete, do something with the results._

for (uint8_t i = 0; i < SONAR_NUM; i++) {
_ Serial.print(i);
Serial.print("cm ");

PLease enclose your code in CODE tags, easier to read.

Take a look at the LCD libraries examples, they show how to display text on LCD's.

Set your cursor to Row 0, Column 0, then Display "Distance1: " Move your cursor over a bit, then display that value. Set the cursor to Row 1, Column 0, and repeat.

lcd.print("Dist1: ");
lcd.print("Dist2: ");

There are probably more elegant ways to do this, but that works for me.

Thank you, will try this tomorrow and let you know if it works

I don't think this will work actually, as val1 and val2 as you described are both being output as "i".

So at the moment they are being out put to serial.monitor as

"0=5cm 1=0cm "

So I need a way to take the 5cm value and 0cm value and output just those onto an LCD.

The way this works is it displays "0=5cm" then "1=0cm" then starts a new line.

Post your code, in code tags, as it is now.

What I wrote was just an example, you can't take it verbose.