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('PmodAD1','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('PmodHB3','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('PmodISNS20','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('PmodMIC3','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('PmodOLEDrgb','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('PmodTC1','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