Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 1 month ago.
I would like assistence with my first i2c project(circuit and coding)
I have an LPC1768, Atlas-Scientific PH sensor and an Atlas-Scientific power-isolator(pwr-iso).
Data sheets-
- ph sensor: http://www.atlas-scientific.com/_files/_datasheets/_circuit/pH_EZO_datasheet.pdf?
- pwr-iso: http://www.atlas-scientific.com/_files/_datasheets/_circuit/pwr-iso.pdf?
Circuit design-
- mbed USB VCC to the pwr-iso IN vcc
- mbed GND to pwr-iso IN gnd
- mbed SDA to pwr-iso IN A
- mbed SCL to pwr-iso IN B
- pwr-iso OUT 5v to ph sensor VCC
- pwr-iso OUT GND to ph sensor GND
- pwr-iso OUT A to ph sensor SDA
- pwr-iso OUT B to ph sensor SCL
The datasheet for the ph sensor recommends 4.7k pullup resisters; however, the pwr-iso board I think already has 10k pullups so I didn't use 4.7k pull up resisters.
For a basic demo, I would like to send the command R,25.3 to the ph sensor, wait 1second, then output the incoming results.
I attempted to write code to achieve the basic demo.
When the code executes I think data is being sent to the ph sensor because the ph sensor blinks the red LEDs which means the command produced an error. I am not sure if I am receiving data because there isn't any data in the buffer string(so I think). I am not sure if that has anything to do with my code and/or with my circuit design.
The ph sensor datasheet explains, The first byte of the data read back, is the response code. This byte informs the master of the status of the data about to be read back. For all commands, the first byte of the read either 255 or 254 or 1 or 2.
When my code sends a read request the red led blinks so I'm sure something is being sent to the mbed. I just don't know how to properly send data and receive the data.
I browsed over the i2c example in the Handbook and I remember it stated, "The mbed API uses 8 bit addresses, so make sure to take that 7 bit address and left shift it by 1 before passing it.", which I have obsolutely no idea what that means and if that is the issue.
Any help is greatly appreciated.
Code:
#include "mbed.h" #include <string> Serial pc(USBTX, USBRX); // tx, rx I2C i2c(p28, p27); const int addr = 0x63; int main() { char buff[8]; //Request PH value at temp 25.3 degrees C const char data[]="R,25.3"; string buffer; //Test output of serial terminal pc.printf("Writing: R,25.3 to i2c\r\n"); //Send a Read request to PH sensor i2c.write(addr, data, 1); //Wait 1second wait(1); //After 1second read and store received data to buff array i2c.read(addr, buff, 8); //Slow it up a bit wait(1); pc.printf("Going to Output data in...\r\n"); wait(1); pc.printf("3...\r\n"); wait(1); pc.printf("2...\r\n"); wait(1); pc.printf("1...\r\n"); wait(1); //Loop through buff array and store to buffer string for (int i=0; i<sizeof(buff - 1); i++) { //buff[i] = -1; buffer += buff[i]; } //Slow it up a bit wait(1); //Output the buffer string to terminal window pc.printf("data: %s\r\n", buffer.c_str()); while(1) { wait(20); pc.printf("Ping..\r\n"); } }
UPDATE I changed
const int addr = 0x63;
to
const int addr = 0x63 << 1;
I am now able to send the command successfully.
UPDATE my new condensed code:
#include "mbed.h" #include <string> Serial pc(USBTX, USBRX); // tx, rx I2C i2c(p28, p27); const int addr = 0x63 << 1; int main() { while(1) { wait(3); //Request PH value at temp 25.3 degrees C char data[]="R,25.3"; char phdata[7]; //Test output of serial terminal pc.printf("Writing: R,25.3 to i2c\r\n"); //Send a Read request to PH sensor i2c.write(addr, data, 1); //Wait 1second wait(1); //After 1second read and store received data to phval array i2c.read(addr, phdata, 7); //Slow it up a bit wait(1); //Loop through buff array and store to buffer string for (int i=0; i<sizeof(phdata); i++) { float arrayval = phdata[i]; pc.printf("buff: %.0f\r\n", arrayval); } } }
I can print out the ASCII code numbers. How do I convert the ASCII code to its corresponding character?
UPDATED CODE
#include "mbed.h" #include <string> Serial pc(USBTX, USBRX); // tx, rx I2C i2c(p28, p27); const int addr = 0x63 << 1; int main() { while(1) { wait(1); //Request PH value at temp 25.3 degrees C char data[] = "R,25.3"; char rawdata[10]; string phValue; //Test output of serial terminal pc.printf("Writing: R,25.3 to i2c\r\n"); //Send a Read request to PH sensor i2c.write(addr, data, 1); //Wait 1second so PH module can get a result wait(1); //After 1second read and store received data to phval array i2c.read(addr, rawdata, 10); //Slow it up a bit wait(0.5); //Loop through rawdata char array and store to phValue string for (int i=0; i<sizeof(rawdata); i++) { if (rawdata[i] != 1 && rawdata[i] != 255 && rawdata[i] != 254 && rawdata[i] != 0) { //if rawdata doesn't == 1 or 255 or 254 or 0 then concatenate to phValue string phValue += rawdata[i]; } } //Output full PH Value pc.printf("Value: %s\r\n", phValue.c_str()); //Trim last number from PH Value X.XXX phValue.resize(phValue.size()-1); //Output trimmed PH Value X.XX pc.printf("Value: %s\r\n", phValue.c_str()); } }
1 Answer
10 years, 1 month ago.
I don't know if the rest of your code is correct, however the address part is simply changing it into: (0x63 << 1). So literally one shifted to the left.
you removed the code that print out the characters in ascii form.
Add the line:
how to print characters
also increase the size of the phdata array from 7 characters to something that exceeds the size of the expected text.
after your read and you should see the characters.
posted by Geoffrey Lansberry 13 Nov 2014I was able to see the characters. I also updated my original post with my updated code
posted by d 0773d 14 Nov 2014