DB25 connector pinouts.

PC printer Port Reads Arduino Sensors

by Lewis Loflin

This first test program we will learn how to interface a PC printer port to read a DS18B20 Centigrade temperature sensor though an Arduino microcontroller.

The purpose of these series of projects is to use the Arduino microcontroller to operate low-level electronics but use a high level language like Python to control Arduino from a common PC.

The goal was to control and read complex sensors, operate stepper motors, etc. though the PC printer port or Raspberry Pi. This code with modifications works on Raspberry Pi and vice-versa.

I wanted to avoid the use of I2c and complex C programming with the PC. This takes advantage of my modified version of pyparallel. See Programming the PC Printer Port in Python.

Arduino is programmed in a type of C and can operate a vast number of sensors and motors, but is worthless for high-level programming better done in Python.

Python is handy as it will operate much the same with any PC without getting into low-level architecture. This bridges the gap between using a nice graphical interface with python-tkinter and the electronics portion of Arduino.

The wiring diagram is shown above and was connected to an Arduino Nano - should work with any of them.

The projects will get more and more complex leading the a combination Arduino with read-write capability to the PC and the PC operating a 2X16 LCD display in conjunction with Arduino. This program uses pportArduino1.ino or pportArduino2.ino.

#!/usr/bin/env python 
# File pportArduinoRead.py
# http://www.bristolwatch.com/pport/index.htm
# By Lewis Loflin - lewis@bvu.net
# Read serial data from Arduino
# Same as TLC548 except 16 bit
# 

# Use reset with Arduino sketch pportArduino1.ino

# Reset not used with Arduino sketch
# pportArduino2.ino
# Reset only once for pportArduino2.ino
# which will return integer from DS18B20
# temperature sensor. Will convert to F and C.
# Arduino data displayed on terminal.

# SW1 is connected from ground to DB25 pin 10 and pulled high
# by a 10k resistor.

import parallel
import time

p = parallel.Parallel()


# init i/o pins
p.setDataStrobe(0) # Pin 1 output to CLK DP6 Arduino
p.setAutoFeed(0)   # Pin 14 output NC
p.getInError()	   # Pin 15 input to DATA OUT Arduino DP4
p.setInitOut(0)    # Pin 16 output to CS-NOT DP5 Arduino
p.setSelect(0)     # Pin 17 output Arduino reset

# data LSB first
# enable CS1 HIGH
# set CLK HIGH
# read data bit shift left
# set CLK LOW
# repeat 15 times
# then once more after loop.
# last bit should not be shifted.

# Arduino
#define CLK 6
#define CS1 5
#define dataBit 4




# Reset Arduino to perform whatever reading
# use transistor between pport and Arduino
def resetArduino():
    p.setSelect(1)
    time.sleep(.001)
    p.setSelect(0)
	time.sleep(3)
    


def readArduino():
        temp = 0
        p.setInitOut(1) # enable CS
        time.sleep(.001)
        for i in range(0,15): # loop here 7 times
                p.setDataStrobe(1) # CLK HIGH
                time.sleep(.001)
                temp = temp + p.getInError() # get bit
                temp = temp << 1 # shift 1 bit left
                p.setDataStrobe(0) # CLK LOW
        p.setDataStrobe(1) # CLK HIGH
        time.sleep(.001)
        temp = temp + p.getInError() # get bit 8 
        p.setDataStrobe(0) # CLK LOW
        p.setInitOut(0) # CS LOW
        return temp




#####################################################

# Print a 16-bit number (integer) in binary. 
# Returns string.
# unlike python bin() this doesn't drop leading zeros
def convBinary(value):
    binaryValue = 'b'
    for  x in range(0, 16):
        temp = value & 0x8000
        if temp == 0x8000:
           binaryValue += '1'
        else:
            binaryValue += '0'
        value = value << 1
    return binaryValue

####################################################  
    
	
# SW1 is connected from ground to DB25 pin 10 and pulled high
# by a 10k resistor.
def SW1():
	return p.getInAcknowledge()  
   # check pin 10 DB25 when pressed returns 0



def degC(val):
	return val * 0.0625
	
def degF(val):
	return (val * 0.0625) * 9 / 5 + 32

# Use Arduino sketch pportArduino1.ino
	
resetArduino() 

print "Please wait for reading..."

time.sleep(.5)

j = readArduino()

print hex(j)
print bin(j)
print j

print degC(j), "deg. C"

print degF(j), "deg. F"

print "Exit now."      
        
exit


Download pport-1.0.iso from Sourceforge.com then burn to DVD (file size 920 meg.), insert into DVD drive and reboot. Make sure PC is set to boot from DVD ROM.

This is pre-configured by myself to use Python to control the printer port. Python can be run from IDLE or Geany.

All of my PPORT electronics projects will work without installation to a PC.

Programs can be saved to thumb drive in LIVE mode.

Projects

Below are listed a series of projects using pyparallel and electronics. Starting with routines I wrote to aid students I'd advise walking through this in sequence. Have fun and send comments and/or corrections to lewis@bvu.net.

Linux Videos

Printer Port in C


Web site Copyright Lewis Loflin, All rights reserved.
If using this material on another site, please provide a link back to my site.