Interfacing Mcp3208 With Raspberry Pi
- I am using Raspberry Pi 2 board with raspbian loaded. need to do SPI by bit banging & interface
MCP3208
. I have taken code from Github. It is written for MCp3008(10 bit adc).
Only change I made in code is that instead of calling:
adcValue = recvBits(12, clkPin, misoPin)
May 18, 2016 - This tutorial goes through the process of setting up a Raspberry Pi ADC. This particular chip makes use of the SPI (Serial Peripheral interface.
I called adcValue = recvBits(14, clkPin, misoPin)
since have to receive 14 bits of data.
Problem: It keeps on sending random data ranging from 0-10700. Even though data should be max 4095. It means I am not reading data correctly.
I think the problem is that MCP3208 has max freq = 2Mhz, but in code there is no delay between two consecutive data read or write. I think I need to add some delay of 0.5us whenever I need to transition clock since I am operating at 1Mhz.
For a small delay I am currently reading Accurate Delays on the Raspberry Pi
Excerpt:
...when we need accurate short delays in the order of microseconds, it’s not always the best way, so to combat this, after studying the BCM2835 ARM Peripherals manual and chatting to others, I’ve come up with a hybrid solution for wiringPi. What I do now is for delays of under 100μS I use the hardware timer (which appears to be otherwise unused), and poll it in a busy-loop, but for delays of 100μS or more, then I resort to the standard nanosleep(2) call.
Palu Macil1 Answer
I finally found some py code to simplify reading from the 3208 thanks to RaresPlescan.https://github.com/RaresPlescan/daisypi/blob/master/sense/mcp3208/adc_3.py
I had a data logger build on the pi, that was using a 3008. The COTS data logger I was trying to replicate had better resolution, so I started looking for a 12 bit and found the 3208. I literally swapped the 3008 out for the 3208 and with this guys code I have achieved better resolution than the COTS data logger.
Not the answer you're looking for? Browse other questions tagged raspberry-pispi or ask your own question.
Hardware components | |||||
| × | 1 | |||
| × | 1 | |||
| × | 1 | |||
Software apps and online services | |||||
|
Story
How to interface Arduino with RaspberryPi
1. Download and install Arduino in Rpi
Open browser in raspberry pi and open the link below.
Then, download Linux ARM and extract it by command.
Mcp3208 Interface With Raspberry Pi Python
After extracting you will see new directory. Here I am using arduino-1.8.1 IDE. Then go to directory by using command.
To run Arduino, use command in arduino-1.8.1 directory.
2. How to use libraries
To install any libraries in Arduino , simply download the library and paste in arduino--->libraries folder .
NOTE:- Make sure there is no ( - ) in library folder for ex (DHT-sensor). If there is any (-),rename it .
Now let's do one example of temperature sensor. To check whether its working or not.
Here, I am interfacing DHT11 temperature sensor with Arduino uno.
**************************************************************************************************
Arduino Program
**************************************************************************************************
#include 'DHT.h'
#define DHTPIN 7 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
int h = dht.readHumidity();
// Read temperature as Celsius (the default)
int t = dht.readTemperature();
Serial.print('Humidity: ');
Serial.print(h);
Serial.print('t'); // for splitting
Serial.print('Temperature: ');
Serial.print(t);
Serial.print('n'); // for new line
Mcp3204 Raspberry Pi
}
*******************************************************************************************************
Verify it and upload it to Arduino uno via Raspberry pi.Then open serial monitor .
We have verified its working ,now proceed to next step.
3. Store temperature data to database of raspberry pi .
Here I am assuming you already have created database in raspberry pi. If you dont know how to create database,check link below.
After that install the required libraraies.
After installing libraries. Create a python script to store data from Arduino to Raspberry pi database.
Program to be write in script
*****************************************************************************************************
import serial
import time
import MySQLdb as mdb
arduino = serial.Serial('/dev/ttyACM0') // make sure you write correct serial
arduino.baudrate=9600
data = arduino.readline()
time.sleep(2)
data = arduino.readline()
pieces =data.split('t')
temperature = pieces[0]
humidity = pieces[1]
con = mdb.connect('localhost','root','password','database_name');
with con:
cursor =con.cursor()
cursor.execute('INSERT INTO table_name VALUES(',%s,%s
(temperature,humidity))
con.commit()
cursor.close()
*******************************************************************************************************
Run python script by command
To run python script automatically after few seconds to update database ,we have to change crontab and save it . To open crontab ,write command.
After opening it ,write */1 * * * * python /home/pi/scriptname.py at the end .
After edit and save crontab. Reboot raspberry pi.
Result
NOTE:- ALL THE THINGS WORK ONLY WHEN TEMPERATURE SENSOR IS CONNECTED TO ARDUINO AND ARDUINO IS CONNECTED TO RASPBERRY PI SERIALLY.
Read moreSchematics
arduino-dht113pindht11wiringdiagram_ytOJpEGUwv.png
Ruchir Sharma
Comments are closed.