Update ADC module.

update adc module ,add ads7830.
This commit is contained in:
Suhayl
2020-03-11 16:06:06 +08:00
parent 01075b8d39
commit a2ac6a4988
45 changed files with 1399 additions and 803 deletions
-35
View File
@@ -1,35 +0,0 @@
/**********************************************************************
* Filename : ADC.c
* Description : ADC and DAC
* Author : www.freenove.com
* modification: 2019/12/27
**********************************************************************/
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#define address 0x48 //pcf8591 default address
#define pinbase 64 //any number above 64 (according to the wiringPi library)
#define A0 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 3
int main(void){
int value;
float voltage;
printf("Program is starting ... \n");
wiringPiSetup(); //Initialize wiringPi.
pcf8591Setup(pinbase,address);
while(1){
value = analogRead(A0); //read analog value of A0 pin
analogWrite(pinbase+0,value);
voltage = (float)value / 255.0 * 3.3; // Calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage);
delay(100);
}
}
+39
View File
@@ -0,0 +1,39 @@
/**********************************************************************
* Filename : ADC.cpp
* Description : Use ADC module to read the voltage value of potentiometer.
* Author : www.freenove.com
* modification: 2020/03/06
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <ADCDevice.hpp>
ADCDevice *adc; // Define an ADC Device class object
int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");
if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
while(1){
int adcValue = adc->analogRead(0); //read analog value of A0 pin
float voltage = (float)adcValue / 255.0 * 3.3; // Calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",adcValue,voltage);
delay(100);
}
return 0;
}
-39
View File
@@ -1,39 +0,0 @@
/**********************************************************************
* Filename : Softlight.c
* Description : Potentiometer control LED
* Author : www.freenove.com
* modification: 2019/12/27
**********************************************************************/
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#include <softPwm.h>
#define address 0x48 //pcf8591 default address
#define pinbase 64 //any number above 64
#define A0 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 3
#define ledPin 0
int main(void){
int value;
float voltage;
printf("Program is starting ... \n");
wiringPiSetup();
softPwmCreate(ledPin,0,100);
pcf8591Setup(pinbase,address);
while(1){
value = analogRead(A0); //read analog value of A0 pin
softPwmWrite(ledPin,value*100/255);
voltage = (float)value / 255.0 * 3.3; // calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage);
delay(100);
}
return 0;
}
@@ -0,0 +1,44 @@
/**********************************************************************
* Filename : Softlight.cpp
* Description : Use potentiometer to control LED
* Author : www.freenove.com
* modification: 2020/03/07
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <softPwm.h>
#include <ADCDevice.hpp>
#define ledPin 0
ADCDevice *adc; // Define an ADC Device class object
int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");
if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
wiringPiSetup();
softPwmCreate(ledPin,0,100);
while(1){
int adcValue = adc->analogRead(0); //read analog value of A0 pin
softPwmWrite(ledPin,adcValue*100/255); // Mapping to PWM duty cycle
float voltage = (float)adcValue / 255.0 * 3.3; // Calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",adcValue,voltage);
delay(30);
}
return 0;
}
@@ -1,40 +1,46 @@
/**********************************************************************
* Filename : ColorfulSoftlight.c
* Description : Potentiometer control RGBLED
* Filename : Softlight.cpp
* Description : Use potentiometer to control LED
* Author : www.freenove.com
* modification: 2019/12/27
* modification: 2020/03/07
**********************************************************************/
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#include <softPwm.h>
#define address 0x48 //pcf8591 default address
#define pinbase 64 //any number above 64
#define A0 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 3
#include <ADCDevice.hpp>
#define ledRedPin 3 //define 3 pins for RGBLED
#define ledGreenPin 2
#define ledBluePin 0
ADCDevice *adc; // Define an ADC Device class object
int main(void){
int val_Red,val_Green,val_Blue;
adc = new ADCDevice();
printf("Program is starting ... \n");
if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
wiringPiSetup();
softPwmCreate(ledRedPin,0,100); //creat 3 PMW output pins for RGBLED
softPwmCreate(ledGreenPin,0,100);
softPwmCreate(ledBluePin,0,100);
pcf8591Setup(pinbase,address); //initialize PCF8591
while(1){
val_Red = analogRead(A0); //read analog value of 3 potentiometers
val_Green = analogRead(A1);
val_Blue = analogRead(A2);
int val_Red = adc->analogRead(0); //read analog value of 3 potentiometers
int val_Green = adc->analogRead(1);
int val_Blue = adc->analogRead(2);
softPwmWrite(ledRedPin,val_Red*100/255); //map the read value of potentiometers into PWM value and output it
softPwmWrite(ledGreenPin,val_Green*100/255);
softPwmWrite(ledBluePin,val_Blue*100/255);
-39
View File
@@ -1,39 +0,0 @@
/**********************************************************************
* Filename : Nightlamp.c
* Description : Photoresistor control LED
* Author : www.freenove.com
* modification: 2019/12/27
**********************************************************************/
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#include <softPwm.h>
#define address 0x48 //pcf8591 default address
#define pinbase 64 //any number above 64
#define A0 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 3
#define ledPin 0
int main(void){
int value;
float voltage;
printf("Program is starting ... \n");
wiringPiSetup();
softPwmCreate(ledPin,0,100);
pcf8591Setup(pinbase,address);
while(1){
value = analogRead(A0); //read analog value of A0 pin
softPwmWrite(ledPin,value*100/255);
voltage = (float)value / 255.0 * 3.3; // calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage);
delay(100);
}
return 0;
}
@@ -0,0 +1,44 @@
/**********************************************************************
* Filename : Nightlamp.cpp
* Description : Photoresistor control LED
* Author : www.freenove.com
* modification: 2020/03/09
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <softPwm.h>
#include <ADCDevice.hpp>
#define ledPin 0
ADCDevice *adc; // Define an ADC Device class object
int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");
if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
wiringPiSetup();
softPwmCreate(ledPin,0,100);
while(1){
int value = adc->analogRead(0); //read analog value of A0 pin
softPwmWrite(ledPin,value*100/255);
float voltage = (float)value / 255.0 * 3.3; // calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage);
delay(100);
}
return 0;
}
@@ -1,39 +0,0 @@
/**********************************************************************
* Filename : Thermometer.c
* Description : DIY Thermometer
* Author : www.freenove.com
* modification: 2019/12/27
**********************************************************************/
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#include <math.h>
#define address 0x48 //pcf8591 default address
#define pinbase 64 //any number above 64
#define A0 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 3
int main(void){
int adcValue;
float tempK,tempC;
float voltage,Rt;
printf("Program is starting ... \n");
wiringPiSetup();
pcf8591Setup(pinbase,address);
while(1){
adcValue = analogRead(A0); //read analog value A0 pin
voltage = (float)adcValue / 255.0 * 3.3; // calculate voltage
Rt = 10 * voltage / (3.3 - voltage); //calculate resistance value of thermistor
tempK = 1/(1/(273.15 + 25) + log(Rt/10)/3950.0); //calculate temperature (Kelvin)
tempC = tempK -273.15; //calculate temperature (Celsius)
printf("ADC value : %d ,\tVoltage : %.2fV, \tTemperature : %.2fC\n",adcValue,voltage,tempC);
delay(100);
}
return 0;
}
@@ -0,0 +1,43 @@
/**********************************************************************
* Filename : Thermometer.cpp
* Description : DIY Thermometer
* Author : www.freenove.com
* modification: 2020/03/09
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <math.h>
#include <ADCDevice.hpp>
ADCDevice *adc; // Define an ADC Device class object
int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");
if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
printf("Program is starting ... \n");
while(1){
int adcValue = adc->analogRead(0); //read analog value A0 pin
float voltage = (float)adcValue / 255.0 * 3.3; // calculate voltage
float Rt = 10 * voltage / (3.3 - voltage); //calculate resistance value of thermistor
float tempK = 1/(1/(273.15 + 25) + log(Rt/10)/3950.0); //calculate temperature (Kelvin)
float tempC = tempK -273.15; //calculate temperature (Celsius)
printf("ADC value : %d ,\tVoltage : %.2fV, \tTemperature : %.2fC\n",adcValue,voltage,tempC);
delay(100);
}
return 0;
}
-41
View File
@@ -1,41 +0,0 @@
/**********************************************************************
* Filename : Joystick.c
* Description : Read Joystick
* Author : www.freenove.com
* modification: 2019/12/27
**********************************************************************/
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#include <softPwm.h>
#define address 0x48 //pcf8591 default address
#define pinbase 64 //any number above 64
#define A0 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 3
#define Z_Pin 1 //define pin for axis Z
int main(void){
int val_X,val_Y,val_Z;
printf("Program is starting ... \n");
wiringPiSetup();
pinMode(Z_Pin,INPUT); //set Z_Pin as input pin and pull-up mode
pullUpDnControl(Z_Pin,PUD_UP);
pcf8591Setup(pinbase,address); //initialize PCF8591
while(1){
val_Z = digitalRead(Z_Pin); //read digital value of axis Z
val_Y = analogRead(A0); //read analog value of axis X and Y
val_X = analogRead(A1);
printf("val_X: %d ,\tval_Y: %d ,\tval_Z: %d \n",val_X,val_Y,val_Z);
delay(100);
}
return 0;
}
+46
View File
@@ -0,0 +1,46 @@
/**********************************************************************
* Filename : Joystick.cpp
* Description : Read Joystick
* Author : www.freenove.com
* modification: 2020/03/09
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <softPwm.h>
#include <ADCDevice.hpp>
#define Z_Pin 1 //define pin for axis Z
ADCDevice *adc; // Define an ADC Device class object
int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");
if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
wiringPiSetup();
pinMode(Z_Pin,INPUT); //set Z_Pin as input pin and pull-up mode
pullUpDnControl(Z_Pin,PUD_UP);
while(1){
int val_Z = digitalRead(Z_Pin); //read digital value of axis Z
int val_Y = adc->analogRead(0); //read analog value of axis X and Y
int val_X = adc->analogRead(1);
printf("val_X: %d ,\tval_Y: %d ,\tval_Z: %d \n",val_X,val_Y,val_Z);
delay(100);
}
return 0;
}
-70
View File
@@ -1,70 +0,0 @@
/**********************************************************************
* Filename : Motor.c
* Description : Control Motor by L293D
* Author : www.freenove.com
* modification: 2019/12/27
**********************************************************************/
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#include <softPwm.h>
#include <math.h>
#include <stdlib.h>
#define address 0x48 //pcf8591 default address
#define pinbase 64 //any number above 64
#define A0 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 3
#define motorPin1 2 //define the pin connected to L293D
#define motorPin2 0
#define enablePin 3
//Map function: map the value from a range to another range.
long map(long value,long fromLow,long fromHigh,long toLow,long toHigh){
return (toHigh-toLow)*(value-fromLow) / (fromHigh-fromLow) + toLow;
}
//motor function: determine the direction and speed of the motor according to the ADC
void motor(int ADC){
int value = ADC -128;
if(value>0){
digitalWrite(motorPin1,HIGH);
digitalWrite(motorPin2,LOW);
printf("turn Forward...\n");
}
else if (value<0){
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,HIGH);
printf("turn Back...\n");
}
else {
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
printf("Motor Stop...\n");
}
softPwmWrite(enablePin,map(abs(value),0,128,0,100));
printf("The PWM duty cycle is %d%%\n",abs(value)*100/127);//print the PMW duty cycle
}
int main(void){
int value;
printf("Program is starting ... \n");
wiringPiSetup();
pinMode(enablePin,OUTPUT);//set mode for the pin
pinMode(motorPin1,OUTPUT);
pinMode(motorPin2,OUTPUT);
softPwmCreate(enablePin,0,100);//define PMW pin
pcf8591Setup(pinbase,address);//initialize PCF8591
while(1){
value = analogRead(A0); //read analog value of A0 pin
printf("ADC value : %d \n",value);
motor(value); //make the motor rotate with speed(analog value of A0 pin)
delay(100);
}
return 0;
}
+76
View File
@@ -0,0 +1,76 @@
/**********************************************************************
* Filename : Motor.cpp
* Description : Control Motor by L293D
* Author : www.freenove.com
* modification: 2020/03/09
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <softPwm.h>
#include <math.h>
#include <stdlib.h>
#include <ADCDevice.hpp>
#define motorPin1 2 //define the pin connected to L293D
#define motorPin2 0
#define enablePin 3
ADCDevice *adc; // Define an ADC Device class object
//Map function: map the value from a range to another range.
long map(long value,long fromLow,long fromHigh,long toLow,long toHigh){
return (toHigh-toLow)*(value-fromLow) / (fromHigh-fromLow) + toLow;
}
//motor function: determine the direction and speed of the motor according to the ADC
void motor(int ADC){
int value = ADC -128;
if(value>0){
digitalWrite(motorPin1,HIGH);
digitalWrite(motorPin2,LOW);
printf("turn Forward...\n");
}
else if (value<0){
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,HIGH);
printf("turn Back...\n");
}
else {
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
printf("Motor Stop...\n");
}
softPwmWrite(enablePin,map(abs(value),0,128,0,100));
printf("The PWM duty cycle is %d%%\n",abs(value)*100/127);//print the PMW duty cycle
}
int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");
if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
wiringPiSetup();
pinMode(enablePin,OUTPUT);//set mode for the pin
pinMode(motorPin1,OUTPUT);
pinMode(motorPin2,OUTPUT);
softPwmCreate(enablePin,0,100);//define PMW pin
while(1){
int value = adc->analogRead(0); //read analog value of A0 pin
printf("ADC value : %d \n",value);
motor(value); //make the motor rotate with speed(analog value of A0 pin)
delay(100);
}
return 0;
}
+23 -20
View File
@@ -1,38 +1,41 @@
#!/usr/bin/env python3
#############################################################################
# Filename : ADC.py
# Description : Analog and Digital Conversion, ADC and DAC
# Author : www.freenove.com
# modification: 2019/12/27
########################################################################
import smbus
# Filename : ADC.py
# Description : Use ADC module to read the voltage value of potentiometer.
# Author : www.freenove.com
# modification: 2020/03/06
########################################################################
import time
from ADCDevice import *
address = 0x48 # default address of PCF8591
bus=smbus.SMBus(1)
cmd=0x40 # command, 0100 0000
adc = ADCDevice() # Define an ADCDevice class object
def analogRead(chn): # read ADC value,chn:0,1,2,3
value = bus.read_byte_data(address,cmd+chn)
return value
def analogWrite(value): # write DAC value
bus.write_byte_data(address,cmd,value)
def setup():
global adc
if(adc.detectI2C(0x48)): # Detect the pcf8591.
adc = PCF8591()
elif(adc.detectI2C(0x4b)): # Detect the ads7830
adc = ADS7830()
else:
print("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
exit(-1)
def loop():
while True:
value = analogRead(0) # read the ADC value of channel 0
analogWrite(value) # write the DAC value to control led
value = adc.analogRead(0) # read the ADC value of channel 0
voltage = value / 255.0 * 3.3 # calculate the voltage value
print ('ADC Value : %d, Voltage : %.2f'%(value,voltage))
time.sleep(0.01)
time.sleep(0.1)
def destroy():
bus.close()
adc.close()
if __name__ == '__main__': # Program entrance
print ('Program is starting ... ')
try:
setup()
loop()
except KeyboardInterrupt: # Press ctrl-c to end the program.
destroy()
+24 -26
View File
@@ -1,51 +1,49 @@
#!/usr/bin/env python3
#############################################################################
# Filename : Softlight.py
# Description : Control LED with Potentiometer
########################################################################
# Filename : ADC.py
# Description : Use ADC module to read the voltage value of potentiometer.
# Author : www.freenove.com
# modification: 2019/12/27
# modification: 2020/03/06
########################################################################
import RPi.GPIO as GPIO
import smbus
import time
from ADCDevice import *
address = 0x48 # default address of PCF8591
bus=smbus.SMBus(1)
cmd=0x40 # command, 0100 0000
ledPin = 11
def analogRead(chn):
value = bus.read_byte_data(address,cmd+chn)
return value
def analogWrite(value):
bus.write_byte_data(address,cmd,value)
adc = ADCDevice() # Define an ADCDevice class object
def setup():
global adc
if(adc.detectI2C(0x48)): # Detect the pcf8591.
adc = PCF8591()
elif(adc.detectI2C(0x4b)): # Detect the ads7830
adc = ADS7830()
else:
print("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
exit(-1)
global p
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPin,GPIO.OUT)
GPIO.output(ledPin,GPIO.LOW)
p = GPIO.PWM(ledPin,1000)
p.start(0)
def loop():
while True:
value = analogRead(0) #read ADC value of A0 pin
p.ChangeDutyCycle(value*100/255) #Convert ADC value to duty cycle of PWM
voltage = value / 255.0 * 3.3 #calculate voltage
value = adc.analogRead(0) # read the ADC value of channel 0
p.ChangeDutyCycle(value*100/255) # Mapping to PWM duty cycle
voltage = value / 255.0 * 3.3 # calculate the voltage value
print ('ADC Value : %d, Voltage : %.2f'%(value,voltage))
time.sleep(0.01)
time.sleep(0.03)
def destroy():
bus.close()
GPIO.cleanup()
adc.close()
if __name__ == '__main__': # Program entrance
if __name__ == '__main__': # Program entrance
print ('Program is starting ... ')
setup()
try:
setup()
loop()
except KeyboardInterrupt: # Press ctrl-c to end the program.
destroy()
@@ -3,30 +3,29 @@
# Filename : Softlight.py
# Description : Control RGBLED with Potentiometer
# Author : www.freenove.com
# modification: 2019/12/27
# modification: 2020/03/09
########################################################################
import RPi.GPIO as GPIO
import smbus
import time
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
from ADCDevice import *
ledRedPin = 15 # define 3 pins for RGBLED
ledGreenPin = 13
ledBluePin = 11
def analogRead(chn): # read ADC value
bus.write_byte(address,cmd+chn)
value = bus.read_byte(address)
value = bus.read_byte(address)
return value
def analogWrite(value):
bus.write_byte_data(address,cmd,value)
adc = ADCDevice() # Define an ADCDevice class object
def setup():
global adc
if(adc.detectI2C(0x48)): # Detect the pcf8591.
adc = PCF8591()
elif(adc.detectI2C(0x4b)): # Detect the ads7830
adc = ADS7830()
else:
print("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
exit(-1)
global p_Red,p_Green,p_Blue
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledRedPin,GPIO.OUT) # set RGBLED pins to OUTPUT mode
@@ -42,9 +41,9 @@ def setup():
def loop():
while True:
value_Red = analogRead(0) # read ADC value of 3 potentiometers
value_Green = analogRead(1)
value_Blue = analogRead(2)
value_Red = adc.analogRead(0) # read ADC value of 3 potentiometers
value_Green = adc.analogRead(1)
value_Blue = adc.analogRead(2)
p_Red.ChangeDutyCycle(value_Red*100/255) # map the read value of potentiometers into PWM value and output it
p_Green.ChangeDutyCycle(value_Green*100/255)
p_Blue.ChangeDutyCycle(value_Blue*100/255)
@@ -53,7 +52,7 @@ def loop():
time.sleep(0.01)
def destroy():
bus.close()
adc.close()
GPIO.cleanup()
if __name__ == '__main__': # Program entrance
+15 -14
View File
@@ -3,25 +3,26 @@
# Filename : Nightlamp.py
# Description : Control LED with Photoresistor
# Author : www.freenove.com
# modification: 2019/12/27
# modification: 2020/03/09
########################################################################
import RPi.GPIO as GPIO
import smbus
import time
from ADCDevice import *
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
ledPin = 11 # define ledPin
def analogRead(chn):
value = bus.read_byte_data(address,cmd+chn)
return value
def analogWrite(value):
bus.write_byte_data(address,cmd,value)
adc = ADCDevice() # Define an ADCDevice class object
def setup():
global adc
if(adc.detectI2C(0x48)): # Detect the pcf8591.
adc = PCF8591()
elif(adc.detectI2C(0x4b)): # Detect the ads7830
adc = ADS7830()
else:
print("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
exit(-1)
global p
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledPin,GPIO.OUT) # set ledPin to OUTPUT mode
@@ -32,14 +33,14 @@ def setup():
def loop():
while True:
value = analogRead(0) # read the ADC value of channel 0
value = adc.analogRead(0) # read the ADC value of channel 0
p.ChangeDutyCycle(value*100/255)
voltage = value / 255.0 * 3.3
print ('ADC Value : %d, Voltage : %.2f'%(value,voltage))
time.sleep(0.01)
def destroy():
bus.close()
adc.close()
GPIO.cleanup()
if __name__ == '__main__': # Program entrance
@@ -3,38 +3,39 @@
# Filename : Thermometer.py
# Description : DIY Thermometer
# Author : www.freenove.com
# modification: 2019/12/27
# modification: 2019/03/09
########################################################################
import RPi.GPIO as GPIO
import smbus
import time
import math
from ADCDevice import *
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
adc = ADCDevice() # Define an ADCDevice class object
def analogRead(chn):
value = bus.read_byte_data(address,cmd+chn)
return value
def analogWrite(value):
bus.write_byte_data(address,cmd,value)
def setup():
GPIO.setmode(GPIO.BOARD)
global adc
if(adc.detectI2C(0x48)): # Detect the pcf8591.
adc = PCF8591()
elif(adc.detectI2C(0x4b)): # Detect the ads7830
adc = ADS7830()
else:
print("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
exit(-1)
def loop():
while True:
value = analogRead(0) # read ADC value A0 pin
voltage = value / 255.0 * 3.3 # calculate voltage
Rt = 10 * voltage / (3.3 - voltage) # calculate resistance value of thermistor
value = adc.analogRead(0) # read ADC value A0 pin
voltage = value / 255.0 * 3.3 # calculate voltage
Rt = 10 * voltage / (3.3 - voltage) # calculate resistance value of thermistor
tempK = 1/(1/(273.15 + 25) + math.log(Rt/10)/3950.0) # calculate temperature (Kelvin)
tempC = tempK -273.15 # calculate temperature (Celsius)
tempC = tempK -273.15 # calculate temperature (Celsius)
print ('ADC Value : %d, Voltage : %.2f, Temperature : %.2f'%(value,voltage,tempC))
time.sleep(0.01)
def destroy():
adc.close()
GPIO.cleanup()
if __name__ == '__main__': # Program entrance
+16 -17
View File
@@ -3,39 +3,38 @@
# Filename : Joystick.py
# Description : Read Joystick state
# Author : www.freenove.com
# modification: 2019/12/27
# modification: 2020/03/09
########################################################################
import RPi.GPIO as GPIO
import smbus
import time
from ADCDevice import *
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
Z_Pin = 12 # define Z_Pin
def analogRead(chn): # read ADC value
bus.write_byte(address,cmd+chn)
value = bus.read_byte(address)
value = bus.read_byte(address)
# value = bus.read_byte_data(address,cmd+chn)
return value
def analogWrite(value):
bus.write_byte_data(address,cmd,value)
adc = ADCDevice() # Define an ADCDevice class object
def setup():
global adc
if(adc.detectI2C(0x48)): # Detect the pcf8591.
adc = PCF8591()
elif(adc.detectI2C(0x4b)): # Detect the ads7830
adc = ADS7830()
else:
print("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
exit(-1)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Z_Pin,GPIO.IN,GPIO.PUD_UP) # set Z_Pin to pull-up mode
def loop():
while True:
val_Z = GPIO.input(Z_Pin) # read digital value of axis Z
val_Y = analogRead(0) # read analog value of axis X and Y
val_X = analogRead(1)
val_Y = adc.analogRead(0) # read analog value of axis X and Y
val_X = adc.analogRead(1)
print ('value_X: %d ,\tvlue_Y: %d ,\tvalue_Z: %d'%(val_X,val_Y,val_Z))
time.sleep(0.01)
def destroy():
bus.close()
adc.close()
GPIO.cleanup()
if __name__ == '__main__':
+13 -13
View File
@@ -6,25 +6,26 @@
# modification: 2019/12/27
########################################################################
import RPi.GPIO as GPIO
import smbus
import time
from ADCDevice import *
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
# define the pins connected to L293D
motoRPin1 = 13
motoRPin2 = 11
enablePin = 15
def analogRead(chn):
value = bus.read_byte_data(address,cmd+chn)
return value
def analogWrite(value):
bus.write_byte_data(address,cmd,value)
adc = ADCDevice() # Define an ADCDevice class object
def setup():
global adc
if(adc.detectI2C(0x48)): # Detect the pcf8591.
adc = PCF8591()
elif(adc.detectI2C(0x4b)): # Detect the ads7830
adc = ADS7830()
else:
print("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
exit(-1)
global p
GPIO.setmode(GPIO.BOARD)
GPIO.setup(motoRPin1,GPIO.OUT) # set pins to OUTPUT mode
@@ -58,13 +59,12 @@ def motor(ADC):
def loop():
while True:
value = analogRead(0) # read ADC value of channel 0
value = adc.analogRead(0) # read ADC value of channel 0
print ('ADC Value : %d'%(value))
motor(value)
time.sleep(0.01)
def destroy():
bus.close()
GPIO.cleanup()
if __name__ == '__main__': # Program entrance