Joystick Weather Clock using OLED Display

joystick clock

See how this project was doing after running non stop for fifteen months here

This project has been updated and now saves data to an SD card. The Git Hub code has been updated. I discovered a problem with the sketch that would give an incorrect February calendar  on some years, this problem has been fixed.

Code for this project can be downloaded from my Git Hub page here

If the SD Card is removed to read data while the clock is running, and then returned, the sketch will not be able to read the SD card. Press the Arduino Reset button to restart the sketch and reconnect the Card, the backup data will be uploaded to restore the data on the clock and data will be written to the card.

I wanted to try to build something that used a joystick for control and decided to build a clock with a number of different screens. These screens would be changed using the joystick and the natural choice for display was my small 128×64 OLED. After several weeks of trials I have come up with a clock that has the following specification. All screens are modified in some way, to display additional information, when the joystick button is pressed.


Once the circuit is built the code should be uploaded, although an SD card is optional using one will allow data to be restored after a power failure. The card should be formatted with a program like SDFormatter. When the sketch starts it will beep once, if the SD Card is recognised there will be a second beep. If the two files data.csv and backup.dat are not found these will be written to the card. If the backup file is found there will be a third beep as it is loaded. The small LED attached to pin 12 will flash briefly each time the backup is saved or loaded. The data.csv file adds data each hour and can be read by a spreadsheet program like Excel. The backup.dat file is overwritten each hour and contains up to three days data which is uploaded to the clock after a power failure. After the SD Card has been checked a splash screen is shown followed by the Analog Clock. Scroll through the screens by moving the joystick left or right.

The Display Screens::

analog clock

Screen 1. An analog clock with second hand. The alarm can be set/unset using joystick switch.

digital clock

Screen 2. A digital clock.Time is displayed in hrs/mins with a flashing colon for seconds. The alarm can be set/unset using joystick switch.

alarm set

Screen 3. This page is used to set the alarm time. Joystick Y pot is used to set time. The joystick switch allows both minutes and hours to be set. The underline shows which is being altered.


Screen 4. An event timer. Counts up to A maximum of 99 minutes (it will then stop), it is  started and stopped with joystick switch. This timer will continue to count when another screen is selected, to stop the counter return to this screen and press the joystick switch. A single beep is sounded every 10 minutes and three times when the counter has reached 99 minutes.




Screen 5. Pressure display in milli bars, shows current pressure change direction. When the joystick switch is pressed the reading changes to pascals.

pressure plot

Screen 6. A 24 hour graph of pressure. Clicking the switch shows last 24 hours and 2 days ago. Midnight starts on the left, 12pm is in the middle of the graph.



Screen 7. A weather forecast, a bit like the old ‘banjo’ barometers! The current pressure is compared to that from the last hourly reading, or if the switch is clicked, from the reading 2 hours ago. The difference in pressure is shown and a forecast based on this is shown.



Screen 8. Current temperature in degrees Celsius, click to show this in Fahrenheit.

temperature plot

Screen 9. 24 hour plot of temperature. and 2 days ago. Midnight starts on the left, 12pm is in the middle of the graph. Currently this is only displayed in degrees Centigrade.

moon phase

moon name

Screen 10. Moon phase, shows graphic and a description. Shows number of days to the next full moon. Clicking on this screen displays the name of this months Full Moon. Names from Medieval English are shown, but the code can be modified to show North American Indian or Chinese names.



child rhyme

Screen 11. Today’s Month and date. Clicking on this shows the appropriate verse from the childrens rhyme ‘Mondays Child’


month rhyme

Screen 12. The month displayed as a calendar. Clicking on this shows the appropriate verse from Sara Colridge’s poem.

In all 22 screens, all easily accessed using the joystick. The code is too large to show here and can be downloaded from my github page here. The github also has an RTC setting sketch and an instruction sketch that prints the instructions on the Serial Monitor.

Components and wiring:

An Arduino Mega is required, because of the program size, but I intend to publish two further blogs with the Barometer and Calendar as standalone sketches that can run on a UNO.

A joystick with switch                        (see this item on ebay)
A Real Time Clock – DS1307               (see this item on ebay)
A pressure sensor – BMP180             (see this item on ebay)
A 128 x 64 OLED display                    (see this item on ebay)
An active buzzer.                                (see this item on ebay)
SD card shield                                     (see this item on ebay)
Logic Level Converter for SD Card   (see this item on ebay)

(The logic level converter is only needed if the SD Card shield does not have a logic level converter chip. The SD card shield shown above does have one).

The SD card shield shown in the link above is a TF SD Card and has a logic Level chip that allows 5 volt signals without using a logic level board.

An active buzzer is required as the code required to operate a passive buzzer will interfere with the OLED display refresh.

The OLED, RTC and BMP are all wired in parallel and Vcc connected to 5 volts, Gnd to Gnd SDA to Mega pin 20 and SCL to Mega pin 21.The Joystick switch is connected to digital pin 2 (pulled high with a 10k resistor). Joystick Gnd to Gnd, Vcc to 5 volts. X Output to Analog pin 1 and joystick output Y to analog pin 0. The Active buzzer is connected to 5 volts, Gnd and digital pin 4. An optional SD  card can be used to store the hourly data in CSV format that can be opened in Excel. 5v connected to Arduino 5v, Gnd to Arduino Gnd (DO NOT CONNECT THE 3.3v PIN ON THE SD CARD. The next three pins needed to be connected via a Logic Level Shifter, MISO to Arduino pin 50, MOSI to Arduino pin 51 and SCLK to Arduino Mega pin 52. Arduino pin 53 is connected directly to the CS pin. All the connection details are in the Sketch. I laid all my components out on a breadboard for testing, but an Arduino Mega prototype shield could be used to make a permanent device.


Use this wiring diagram in conjunction with the wiring instructions in the sketch. Different manufacturers may have different pinouts to those shown here, so check, The OLED will die instantly if connected incorrectly!

The two small blue LEDs sit under the OLED and give a cool blue glow.

The program:

Upload the joystick clock sketch, the sketch sends some debug data to the Serial Monitor (9600, CR/NL selected) if you want to watch this output start the Serial Monitor now. The clock takes a snapshot of the pressure and temperature each hour and saves the data in a string. At midnight this data is moved to a second string (yesterday), the contents of that string is moved to a third string (-48 hours).

The graph displays the current 24 hours data for both pressure and temperature and -24 hours and -48 hours display can be seen by clicking the joystick. Data for the recorded pressure and temperature is stored in memory so if the power is removed, or the Serial Monitor is started the data would be lost. However, the data and a backup file are stored on an SD Card. The hourly data is stored  in a CSV file that can be opened in Excel. The sketch checks for the presence of an SD card on first starting. If one is found data will be stored on the card and any backup data will be uploaded.

If your RTC is already programmed with the correct time and date then the clock is up and running. If the time needs to be adjusted this can be done in one of two ways. Line 188 in the sketch is currently REM’d out (RTC.adjust(DateTime(__DATE__, __TIME__));) to set the clock un REM then upload the sketch. The RTC will be set to your computer time. REM line 188 again and upload this sketch (otherwise the RTC will be reset every time you run the sketch).

After a short Splash Screen the Home Screen is shown. This is an Analog Clock, the current Alarm state is shown and this can be set/unset by clicking the joystick switch. Move the joystick left or right to cycle through the various screens. Only one screen, the Alarm Set screen uses the Y (up/down) direction. To set the alarm, when the screen first opens the Minutes will be displayed with an underline. The minutes can be increased or decreased by moving the joystick up or down.  Click the joystick to move the underline over to the hours, this can then be set as the minutes were set. When the correct time is displayed move the joystick back to display the analog or digital display and set the alarm by clicking the joystick.

The alarm uses an active buzzer, this is pulsed on and off about once a second and the time has been set to run for 10 seconds (although this can easily be changed. The on-board LED is also connected to the alarm code.

The event timer counts up from zero to 99 minutes. Start and stop by clicking the joystick switch.

The barometer has a number of functions, first the current pressure in millibars, or pascals is shown. This is compared with the last reading taken and the direction  (rising, falling or constant) is shown. Just after the hourly reading this direction will almost always show constant, but as time passes this can show rapid changes in pressure between hourly readings.

The next barometer screen shows a graph of pressure readings taken each hour over a 24 hour period. The graph starts at midnight and continues until the last reading at 2300 hours. The data is stored in a string and just before the first reading at midnight the data is moved to a second string, the data from that having been moved to a third string. Data is therefore available for up to a three day period. To see an earlier 24 hour period click the joystick. The small ticks represent four hours. The bottom line is 990mb, the second line up is 1000mb. The next line up is 1010mb and the top line is 1020mb.

The last barometer screen is a Weather Forecast. This is the kind of forecast available on the old fashioned banjo barometers. The last two readings are compared, first to see if the pressure is rising or falling, then the rate of change measured. A forecast is then made based on these results. A result of this is that the forecast can be up to an hour old however, the forecast can often be good indication of weather conditions over the next hour or so. Clicking the joystick while this screen is showing will display the last forecast made and this can often give a better idea of the coming weather. A forecast cannot be made until the second hourly reading has been taken and the earlier forecast will not be available until three readings have been taken,

The next screen screen shows the current temperature in Centigrade, clicking the joystick displays the temperature in Fahrenheit. The temperature over a 24 hour period is shown on the next screen in the same way as the pressure screens. Click to display the last wo days data. The temperature is only displayed in Centigrade and the lower line represents 0C, the next line 10C, 20C and the top line 40C. If the temperature probe is to be placed outdoors the code would need to be altered if temperatures below zero are expected.

A moon Phase Screen is next, showing the phase of the moon in simple graphic format and the number of days to the next full moon. Clicking the joystick will show the name of the current months Full Moon. One of three version, Medieval English, North American Indian and Chinese names are shown, chosen at random.

There are two calendar screens, the first shows the month and day, rather like the old tear off calendar blocks, clicking on this shows the appropriate verse for today from the children’s rhyme Mondays Child. The second screen shows the month in standard calendar format and should be good for some years to come. Clicking on this screen will show the verse for the current month from the poem by Sara Coleridge.

About the joystick:

The joystick I am using has two potentiometers, X and Y, and a switch activated when the joystick is pushed down. With the connector on the left pushing the stick to the right moved from the analog clock to the digital clock. Pushing the stick up increased the values in the on the alarm set screen, for more information see this blog. The switch was very noisy so the code includes a ‘de-bounce’ routine. There is also a small delay in the joystick routines to ensure smooth switching of the screens. Move the joystick either fully left or right then allow it to return to its natural centre before moving it again. Occasionally the screen will skip to the next if the joystick is moved to quickly. The ‘centre’ value is read when the sketch first starts up to allow different units to be used without altering the code.

The Code:

The code consists of a number of routines controlled from the main loop, each screen is given a number and moving the joystick left or right will increase, or decrease, the current screen number. A switch loop is then used to call the required routine. The rest of the main loop checks the alarm time, gathers data each hour and sets or resets various variables and flags.

The use of an SD card is optional, if one is found then the buzzer is sounded twice, otherwise the buzzer only sounds once. The data is saved in CSV format to allow it to be read into a spreadsheet program. Data is saved once an hour as a backup. If this file is available when the program starts the buzzer will sound three times as this data is loaded. This is useful if there has been a temporary loss of power. Be aware that the data may be out of date if the Arduino has been powered down for some time.

Using delay() with U8glib can cause problems with screen refresh, so when a delay is needed (for example in switch de-bounce) the method used in the Arduino example for flashing a LED without using delay() was needed. Interrupt Service Routine is used to detect when the button is pressed and the ISR toggles flags that tell the routines to select alternative screens, or start and stop timers etc. The code is somewhat rambling. but achieves the aim I wanted. There is not much space left on the Mega and most of the diagnostic messages to the Serial Monitor have been REM’d out. There seems to be no problems with stack overflow .

In conclusion:

I had a lot of fun developing this project and intend to build a permanent version. I have extracted some of the routines and converted them to stand alone projects. My favourite routines are the pressure plotting routines and the weather forecast. I deliberately avoided using Progmem, but an optional an SD card can be used to store data.

Flying the weather balloon

The day finally arrived to fly the balloon. From what I could find on the internet each cubic foot of helium will lift 28gms and although it was difficult to confirm, it seemed that the party balloon gas contained about 14 cubic feet of Helium. I needed about 1 kg of lift, so I would need three to four containers of gas. These gas containers were supposed to fill around 50 x 9 inch balloons.

I used a 5kg spring balance to measure the lift and discovered that each container of Helium gave about 250gms of lift and not the calculated 440 gms. I filled the balloon with six containers of gas giving 1.3kg of lift, which as it turned out was a little too much! I purchased a kite winder from Amazon and two spools of a lightweight kite string. This string had a breaking strain of 3 to 4 kg and 300 feet weighed 83 gms. The total weight of balloon, string and electronics package was 733 gms.

The balloon took about 45 minutes to fill and was just under five feet in diameter. The filling  tube was a thicker material than the main balloon and just over an inch in diameter. I used a large rubber bung with a hole in it and attached 2 metres of rubber tube. I connected the rubber tube to the valve on the container directly and did not use the balloon filling valve that is normally used to fill party balloons.

By the time the first container was empty the balloon had started to rise, I tethered it to a table. I measured the lift after each container had emptied by tying a loop in the tether string and a 5kg spring balance was hooked onto this. Pulling the spring balance down and the reading on the balance gave the lift. After five containers of gas had been used the lift was just over 1 kg, but adding in the weight of the balloon this gives 1200 gms, or 240 gms of lift per container of gas. This would have been more than enough to lift my payload, but I decided to add one more container of gas to give a lift of 1300 gms (total 1500 gms with the balloon weight).  The filling tube was folded over and cable ties used to seal it. Kite string was passed through the loop formed and the electronics payload suspended from this loop.

It was surprising how hard it was to pull the ballon down by the string and obvious that for safety sake thick protective glove should be worn to avoid injury from the kite string pulling through the fingers. The electronics package was attached. The wind speed had increased from 2km/hour to about 14km/hour by the time I was ready to fly the balloon and it was extremely difficult to handle. The payload was spinning wildly around the balloon kite string and a completely different means of hanging from the balloon will be needed next time.

The balloon was allowed to rise slowly by controlling the kite string reel, but this needed some effort. The balloon was stopped at about 50 feet to take photographs, but the payload was spinning wildly around the balloon string. Allowing the balloon to rise to about 100 feet had to be stopped as the strong wind was carrying the balloon horizontally as well as upwards. It was decided that it was not safe to continue and the balloon was brought back down to the ground.

The balloon probably had too much lift and probably should have been left at about 1 kg lift. Next time I would not attempt to fly a balloon if the wind speed was above 5km/ hour. To stop the payload spinning wildly I think it should be suspended so that the kite string went through the centre of the payload.


The electronics package hanging from the ballon.

What about the performance of the payload? The electronics consisted of a Raspberry Pi model B+ fitted with. BMP 180 pressure sensor, a DHT11 humidity sensor (temperature could be read from either of theses two). Plugged into the USB sockets was a WiFi dongle, an 8gb memory stick for pictures, a LPRS 900mhz radio and a FTDI adaptor. A UBLOX Neo 6 GPS connected to a logic level converter was connected to the FTDI adaptor. A Raspberry Pi camera was used to take pictures.

A python script was used to collect data from the sensors and send and receive data from the ground station using the LRPS radio. WiFi could be used to control the camera up to about 100 feet which allowed an Android phone (using raspiCAM Remote) to take pictures or BerryCam on an iPad. However, the main control was over the LRPS radio using a VB6 program on a Windows laptop. This program was described in an earlier blog.

One of the planned activities was to measure the height of the balloon using GPS, atmospheric pressure and from the ground by measuring a distance of about 50 feet from the point where the ballon was tethered, the measuring the angle from the ground to the balloon. Trigonometry was used to calculate the height of the balloon. All three methods gave different results!

Photographic results: The spinning of the payload gave some interesting still images. Straight lines were strongly curved and the image blurred as the camera moved. The best result was from video, the camera faced straight down and the violent spinning made watching the video a little uncomfortable. However, some good still frames were extracted and next time I will shoot video at 90 frames per second ( so it can be slowed down to reduce the effects of spinning) and extract stills rather than trying to take still images.

The cost: I purchased the Helium from Amazon at £25 per container and the weather balloons at £3 each, so to launch the balloon costs £153. However, I felt that only five Helium containers were needed reducing the cost to £128. The kite reel, kite string and electronics package are all reusable and cost around £70.

Further thoughts: Wait for still weather, the original plan was to let the balloon rise to 300 or more feet, but with any wind the balloon could travel some way horizontally and become impossible to control. The balloon could be used over several days, if somewhere safe to store it overnight could be found. Some modification of the software is needed, the ability to use 90 GPS for video needs to be added. Using VNC to talk to the Raspberry Pi caused the Python script to close when the Pi lost the WiFi, but experiments have shown that running the Python using SSH may prevent this happening.

Joystick Control with Arduino

This code is available on my GitHub page here

Keyes version is KY023

There are a number of different versions of this and sometimes the pin outs will differ from those shown here. The joystick consists of two potentiometers at right angles, one controlled when the stick is moved in the Y direction (up/down) and the other in the X direction. There is normally a switch operated when the button is pushed. The potentiometers are connected to Vcc and ground and the value is read from the middle pin.

This sketch is a basic way of testing the joystick, later blogs will show how to use the joystick to select items from a menu. The values for X and Y as well as the switch state are printed in the Serial Monitor.

Use this sketch to measure the values of X and Y when the joystick is in its home position in the centre. Up is when the value of Y increases, down is when the value of Y decreases. Left is when the value of X increases, Right is when the value of X decreases.

Joystick Test
prints the output from the joystick on the Serial Monitor

Gnd on Joystick to Gnd on Arduino
Vcc on Joytick to 5 volts on Arduino
VrX on Joystick to A0 on Arduino
VrY on Joystick to A1 on Arduino
Sw on Joystick to pin 2 on Arduino

// definitions for pins
#define xPin 14
#define yPin 15
#define joySwitch 2
#define ledPin 13

// variables
boolean switchState = true; // it will be false when pressed
int xValue = 0;
int yValue = 0;

void setup(void){
pinMode(joySwitch, INPUT_PULLUP); // turn on pullup resistor
pinMode(ledPin, OUTPUT); // onboard LED

void loop (){

// main code will go here and joyStick() is called as a subroutine

void joyStick(){
// this routine reads the joystick values
xValue = analogRead(xPin);
yValue = analogRead(yPin);
switchState = digitalRead(joySwitch);
// remove following lines if not required
Serial.print("X value = ");
Serial.print("Y value = ");
Serial.print("Switch state = ");
if (switchState == false){ // switch pressed
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin, LOW);
delay(1000); // delay to allow data to be seen

Testing RGB LEDs and the KY009 Three Colour LED.

The code is available on my GitHub page here.

Some versions of this board have resisistors in series with the LEDs, some do not. If resistors are not present then use a 390R resistor in series to limit the current.

I was not very impressed with this RGB LED board. It is nothing more than a three colour LED on a PCB, which is not a problem, however neither the Blue or the Red LEDs would work with PWM. This means that it’s not possible to achieve full colour mixing, although some limited mixing can be achieved. I tried a second board and on this one the Red and Blue LEDs would dim with PWM, but the green LED simply switched on and off.

I decided to write a sketch that could be used to test RGB LEDs with an Arduino. This sketch has two parts, the first switches LEDs on and off to provide a rainbow of colours, Red, Green, Yellow, Cyan, Blue and Violet. The second part tests the LEDs ability to fade in and out and each LED is tested separately. Either of the two routines can be switched off by setting either Of the variables rainbow or fader to false.

The LED in its naked form does not give very good colour mixing, so I made a simply diffuser by first cutting a ping pong ball in half then glueing some thin greasproof paper onto the open part of the ball. Place this dome (paper side down) over the RGB LED. An example of the diffuser is shown here.

Connect the common pin to pin 2, the Red LED to pin 3, the Green LED to pin 4 and the Blue LED to pin 5.

This sketch checks a RGB LED to ensure that PWM can be used
to produce a complete range of colours
For best colour mixing results use a diffuser
int gndPin = 2; // the RGB LED common connection
int redLed = 3; // the Red LED
int greenLed = 4; // the green LED
int blueLed = 5; // the blue LED
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// set either of these to false to disable the effect
boolean rainbow = true; // enable the rainbow display
boolean fader = true; // enable the fade display
void setup() {
// declare pin 2 and 3 to be an output:
pinMode(gndPin, OUTPUT);
digitalWrite(gndPin, LOW);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);

void loop(){
if (rainbow){
// Rainbow

for(int r=0; r<3; r++){
analogWrite(redLed, 255); // Red
analogWrite(greenLed, 255); // Yellow
analogWrite(redLed, 0); // Green
analogWrite(blueLed, 128); //Blue/Green
analogWrite(blueLed, 255); //Blue/Green
analogWrite(greenLed, 0); //Blue
analogWrite(redLed, 255); // Violet
analogWrite(redLed, 0); // LEDs all OFF
analogWrite(blueLed, 0);
// the fader effect
brightness = 0;
fadeAmount = 5;
analogWrite(redLed, 0);
brightness = 0;
fadeAmount = 5;
analogWrite(greenLed, 0);
brightness = 0;
fadeAmount = 5;
analogWrite(blueLed, 0);

// the fade routines
void redLedLoop() {
for(int i=0; i<255; i++){
analogWrite(redLed, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
// wait for 30 milliseconds to see the dimming effect

void greenLedLoop(){
for(int i=0; i<255; i++){
analogWrite(greenLed, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
// wait for 30 milliseconds to see the dimming effect

void blueLedLoop(){
for(int i=0; i<255; i++){
analogWrite(blueLed, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
// wait for 30 milliseconds to see the dimming effect

MQ2 Gas Sensor and Arduino


The MQ2 is one of a series of gas detectors and will detect flammable gasses and smoke. The topic of gas sensors is covered in the Arduino Playground here and you should visit this page for more details. This page also lists the various sensors in the range.

This blog records my efforts with this sensor. The small board I purchased from eBay has four pins, Vcc, Gnd, Analog Output and Digital output. Connect Vcc to 5 volts (do not try to power this from an Arduino Digital Output pin as the current drawn is too great. Connect Gnd to the Arduino Gnd. The sensor outputs a voltage proportional to the concentration of the flammable gas, while the Digital Output switches from High to Low when a certain level is reached. The level at which the DO pin switches is set using a small potentiometer on the board. For the sketch below connect the Analog Output to Arduino Analog Pin A0, and the Digital Output pin to Arduino Pin D2. The sketch will show the value of the voltage output and the state of the Digital Output, on the Serial Monitor (9600 baud).

To test the device I soaked a small piece of cotton wool in a flammable solvent and placed this at the bottom of a glass beaker. After five minutes I placed the sensor into the glass beaker and read the outputs from the Serial Monitor. The board I was using had an LED attached to the Digital Output, that came on when the output was greater than 400 (but this can easily be adjusted).

The sensor will get warm in use and can reach 50 or 60*C. I tested my setup by soaking a small piece of cotton wool in a solvent and placing it at the bottom of a glass beaker. After a few minutes I lowered the sensor into the beaker. I found different solvents gave different voltages, for example Ethanol gave a reading of about 250 to 300, while a low boiling petrol fraction gave a reading in excess of 600.

The manufacturer recommends that the sensor is not exposed to solvent vapour for extended periods.

The following sketch prints out the output voltage and state of the Digital Output, a buzzer could be substituted for the LED attached to the Digital output, but this could become a little annoying after a while.

/* GAS Sensor MQ-2
This sensor detects flammable gasses
the board has four pins
connect AO to Arduino pin A0
connect DO to Arduino pin 2
connect Gnd to Arduino Gnd
connect Vcc to Arduino 5 volts

int sensorPin = A0; // select the input pin for the potentiometer
int DOPin = 2; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int ledPin =13;

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(DOPin, INPUT);
pinMode(ledPin, OUTPUT);

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("Analog Output = ");
// turn the ledPin on if triggered
if (digitalRead(DOPin) ==HIGH){
digitalWrite(ledPin, LOW);
Serial.println("Digital Output = OFF");
else {
digitalWrite(ledPin, HIGH);
Serial.println("Digital Output = ON");