3. Basic Examples¶
3.1. AD1¶
12-bit analog-to-digital converter.
3.1.1. Print volts out to the terminal¶
Read ADC channel A1, print the voltage measured out to the terminal, sleep for 0.8s and repeat.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Read ADC channel A1 and print volts out.
"""
from DesignSpark.Pmod.HAT import createPmod
import time
if __name__ == '__main__':
adc = createPmod('AD1','JBA')
time.sleep(0.1)
try:
while True:
volts = adc.readA1Volts()
print(volts)
#val = adc.readA1()
#print(val)
time.sleep(0.8)
except KeyboardInterrupt:
pass
finally:
adc.cleanup()
Requirements
- PmodAD1 module connected to port JBA
- A voltage source connected to ADC channel A1
3.2. HB3¶
2A H-bridge circuit for DC motor drive up to 12V.
3.2.1. Spin motor¶
This example:
- Spins the motor forwards for 20 seconds
- Commands the motor to stop and pauses for 2 seconds
- Spins the motor in reverse for 20 seconds
- Commands the motor to stop and pauses for 2 seconds
- Ramps up the speed across 100 steps in the forward direction
- Ramps down the speed across 100 steps in the forward direction
- Ramps up speed across 100 steps in the reverse direction
- Ramps down the speed across 100 steps in the reverse direction
- Loops back to (1)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Spin motor forwards then backwards.
Ramp speed up and then down in forward direction.
Ramp speed up and then down in reverse direction.
"""
from DesignSpark.Pmod.HAT import createPmod
import time
if __name__ == '__main__':
motor = createPmod('HB3','JAA')
try:
while True:
print('fwd')
motor.forward(20)
time.sleep(2)
motor.stop()
time.sleep(2)
print('rev')
motor.reverse(20)
time.sleep(1)
motor.stop()
time.sleep(2)
print ('ramp up fwd')
for i in range(100):
motor.forward(i)
time.sleep(.1)
print ('ramp down fwd')
for i in range(100):
motor.forward(100-i)
time.sleep(.1)
motor.stop()
time.sleep(2)
print ('ramp up rev')
for i in range(100):
motor.reverse(i)
time.sleep(.1)
print ('ramp down rev')
for i in range(100):
motor.reverse(100-i)
time.sleep(.1)
except KeyboardInterrupt:
pass
finally:
motor.cleanup()
Requirements
- PmodHB3 module connected to port JAA
- DC motor
- Motor power supply
3.3. ISNS20¶
±20A DC or AC input, high accuracy current sensor.
3.3.1. Print milliamps out to the terminal¶
Read the current sense module, print the milliamps out to the terminal, sleep for 0.8s and repeat.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Read current and print out milliamps.
"""
from DesignSpark.Pmod.HAT import createPmod
import time
if __name__ == '__main__':
isens = createPmod('ISNS20','JBA')
time.sleep(0.1)
try:
while True:
mA = isens.readMilliAmps()
print(mA)
time.sleep(0.8)
except KeyboardInterrupt:
pass
finally:
isens.cleanup()
Requirements
- PmodISNS20 module connected to port JBA
- Suitable current source, e.g. a power supply and load
3.4. MIC3¶
Knowles Acoustics SPA2410LR5H-B MEMs microphone and Texas Instrument’s ADCS7476 12-bit Analog-to-Digital Converter.
3.4.1. Display mic level¶
Print a continous sound level reading out to the terminal.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Print a continous sound level reading out.
"""
from DesignSpark.Pmod.HAT import createPmod
import time
s = ':'
lut = [s]
for i in range(128):
s+=':'
lut.append(s)
if __name__ == '__main__':
mic = createPmod('MIC3','JBA')
time.sleep(0.1)
try:
while True:
int = mic.readIntegerValue()
#print(int)
print(lut[int>>5])
snd = mic.readPhysicalValue()
#print(snd)
#time.sleep(0.01)
except KeyboardInterrupt:
pass
finally:
mic.cleanup()
Requirements
- PmodMIC3 module connected to port JBA
3.5. OLEDrgb¶
Organic RGB LED module with a 96×64 pixel display capable of 16-bit color resolution.
3.5.1. Display text in a bounding box¶
Display the text “Hello, World!” in a bounding box.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Display Hello, World! in bounding box.
"""
from DesignSpark.Pmod.HAT import createPmod
from luma.core.render import canvas
from luma.oled.device import ssd1331
if __name__ == '__main__':
try:
oled = createPmod('OLEDrgb','JA')
device = oled.getDevice()
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white", fill="black")
draw.text((16,20), "Hello, World!", fill="white")
while True:
pass
except KeyboardInterrupt:
pass
finally:
oled.cleanup()
Luma.Core & Luma.OLED API:
luma.core.render.canvas
,
luma.oled.device.ssd1331
.
Requirements
- PmodOLEDrgb connected to port JA
3.6. PmodTC1¶
A cold-junction thermocouple-to-digital converter module designed for a classic K-Type thermocouple wire. Features a temperature range of -73°C to 482°C with provided wire.
3.6.1. Print celsius out to the terminal¶
Print the celsius reading out to the terminal, sleep for 0.8s and repeat.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Print the celsius reading out.
"""
from DesignSpark.Pmod.HAT import createPmod
import time
if __name__ == '__main__':
therm = createPmod('TC1','JBA')
time.sleep(0.1)
try:
while True:
cel = therm.readCelcius()
print(cel)
#intn = therm.readInternal()
#print(intn)
time.sleep(0.8)
except KeyboardInterrupt:
pass
finally:
therm.cleanup()
Requirements
- PmodTC1 module connected to port JBA
3.7. PmodACL2¶
A 3-axis MEMS accelerometer module. Features the Analog Devices ADXL362 device with Measurement ranges ±2g, ±4g, ±8g. Up to 12-bit resolution on each axis.
3.7.1. Print axis out to the terminal¶
Print the axis reading out to the terminal, sleep for 0.5s and repeat.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Read and print out accelerometer values.
"""
from DesignSpark.Pmod.HAT import createPmod
import time
if __name__ == '__main__':
ACL2 = createPmod('ACL2','JB')
time.sleep(0.1)
# print(hex(ACL2.getDeviceID())) # Device test. 0xAD will be expected.
# ACL2.setRange(ACL2.SENSOR_RANGE_8G) # default setting: +/- 4G
# maxz = 0
try:
while True:
x,y,z,t = ACL2.getXYZT()
print(x,y,z,t)
time.sleep(0.8)
# if maxz < z:
# maxz = z
except KeyboardInterrupt:
pass
finally:
ACL2.cleanup()
# print(maxz)
Requirements
- PmodACL2 module connected to port JB
3.8. PmodGPS¶
A GlobalTop FGPMMOPA6H GPS antenna module to receive position data from GPS satellites.
3.8.1. Print date, time, and location out to the terminal¶
Print the date, time, and location reading out to the terminal, sleep for 0.5s and repeat.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Read GPS and print position, speed, and GPS time
"""
from DesignSpark.Pmod.HAT import createPmod
import time
# import webbrowser
if __name__ == '__main__':
GPS = createPmod('GPS','JCA') # UART port is only available on JCA
time.sleep(0.1)
try:
while True:
"""
LINE = GPS.getGPSLine()
print(LINE)
time.sleep(0.5)
"""
GPS.gpsUpdate()
print(GPS.getGPSPosData())
time.sleep(0.5)
except KeyboardInterrupt:
pass
finally:
#day,month,year,hour,minute,sec,LatDeg,LogDeg,PDOP = GPS.getGPSPosData()
#url = "https://google.com/maps?q={},{}"
#url = url.format(LatDeg,LogDeg)
GPS.cleanup()
#webbrowser.open(url)
Requirements
- PmodGPS module connected to port JCA
- View of the sky or external antenna connected to the module
- …takes time to obtain a fix and get the data
3.9. PmodSWT¶
Four slides switches for up to 16x different binary logic inputs.
3.9.1. Print switches out to the terminal¶
Read the four switches and print out to the terminal, sleep for 0.5s and repeat.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Read switch status
"""
from DesignSpark.Pmod.HAT import createPmod
import time
if __name__ == '__main__':
SWT = createPmod('SWT','JAA')
time.sleep(0.1)
try:
while True:
print(SWT.GetStatus(1),SWT.GetStatus(2),SWT.GetStatus(3),SWT.GetStatus(4))
time.sleep(0.5)
except KeyboardInterrupt:
pass
finally:
SWT.cleanup()
Requirements
- PmodSWT module connected to port JAA
3.10. PmodLS1¶
A line follower robot interface system board.
3.10.1. Print received signals from optical sensor out to the terminal¶
Print the signals from multiple optical sensors out to the terminal, sleep for 0.5s and repeat.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Read Sensor status
"""
from DesignSpark.Pmod.HAT import createPmod
import time
if __name__ == '__main__':
LS1 = createPmod('LS1','JAA')
time.sleep(0.1)
try:
while True:
print(LS1.GetAllStatus())
time.sleep(0.5)
except KeyboardInterrupt:
pass
finally:
LS1.cleanup()
Requirements
- PmodLS1 module connected to port JAA
3.11. PmodKYPD¶
A 16-button keypad.
3.11.1. Print character out to the terminal¶
Print the key presses out to the terminal, sleep for 0.5s and repeat.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 RS Components Ltd
# SPDX-License-Identifier: MIT License
"""
Print keypad presses
"""
from DesignSpark.Pmod.HAT import createPmod
import time
if __name__ == '__main__':
KYPD = createPmod('KYPD','JA')
time.sleep(0.1)
try:
# set default key map
KYPD.setKeyMapDefault()
# set User Key Map
# keyMap=[['A','B','C','D'],['E','F','G','H'],['I','J','K','L'],['M','N','O','P']]
# KYPD.setKeyMap(keyMap)
# get keyMap
print(KYPD.getKeyMap())
while True:
# print(KYPD.getColRow())
print(KYPD.getKey())
time.sleep(0.5)
except KeyboardInterrupt:
pass
finally:
KYPD.cleanup()
Requirements
- PmodKYPD module connected to port JA