Skip to content

Instantly share code, notes, and snippets.

@amarvutha
Last active December 29, 2015 17:49
Show Gist options
  • Save amarvutha/7706501 to your computer and use it in GitHub Desktop.
Save amarvutha/7706501 to your computer and use it in GitHub Desktop.
Wrapper around uChameleon2 and histogram of ADC readings
from __future__ import division
import serial
import numpy as np
import pylab as plt
from __future__ import division
import serial
import numpy as np
import pylab as plt
class Chameleon:
def __init__(self,portName):
self.connection = serial.Serial(portName,timeout=1)
self.connection.write("id\n")
self.id = self.connection.readline()
print self.id
self.connection.write("led pattern 5\n") # Change led pattern to indicate communication on
def read(self,n):
self.connection.write("adc "+`n`+" v\n")
return float( (1+int(self.connection.readline()[-5:-1].strip(' ')))/4096 )
def pwmStart(self,n):
self.connection.write("pwm "+`n`+" on\n")
def pwmStop(self,n):
self.connection.write("pwm "+`n`+" off\n")
def pwmPeriod(self,n,p):
# Clock is at 24 MHz. Each period is 41.6 ns.
self.connection.write("pwm "+`n`+" period "+`p`+"\n")
def pwmWidth(self,n,w):
self.connection.write("pwm "+`n`+" width "+`w`+"\n")
def pwmOut(self,n,V,p=1000):
w = int(p * V/5.)
self.pwmPeriod(n,p)
self.pwmWidth(n,w)
def close(self):
self.connection.write("led pattern 2\n")
self.connection.close()
# Acquire 2000 voltage readings from channel 5
c = Chameleon('COM7')
V = []
for i in range(2000): V.append(c.read(5))
c.close()
# Plot histogram of acquired voltage values
fig = plt.figure()
ax = fig.add_subplot(111)
n, bins, patches = ax.hist(V, bins=50, histtype='bar')
ax.set_xlabel("Voltage [V]")
ax.set_ylabel("Frequency")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment