PhilTheFish
Posts: 3
Joined: Tue Feb 12, 2013 1:16 pm

Raspberry Pi and PICAXE using i2c

Tue Feb 12, 2013 1:40 pm

Hi
Could anyone help me with the following problem.
I am fairly new to the Raspberry Pi and indeed PICAXE chips. And I am very new to this Forum
I have setup the following:-
I have a PICAXE 20X2 which is counting pulses on its input and storing the value in b0 of the PICAXE scratchpad memory.
I have connected the PICAXE to the i2c bus and can actually see the PICAXE at address 0x50 when I run i2cdetect on my Raspberry Pi.
I have attempeted to read the value of b0 via the i2c bus but it keeps reporting a value of 0 when I know the value is more than this. I am using bus.read_byte_data(0x50,0) but not sure if this is the correct thing to use.
Does anyone have any Python Code that I could try to read this value. I would like to read the value from the PICAXE at minute intervals and store the value in a MySQL Database.
This part is not so much of a problem as I have already achieved this part.
Hope someone can help
Again sorry if the info is a bit lacking but as I say I am new to all this
Regards
Phil

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Raspberry Pi and PICAXE using i2c

Tue Feb 12, 2013 3:28 pm

Similar thing for 8 bit device on I2C

Code: Select all

#!/usr/bin/env python
#
# Basic example of using Python-SMBus and a PCF8574
# Use at your own risk.
#
#  1 Write 0x55 to device
#  2 Check for I/O error (no device connected)
#  3 Read the device
#  4 Display result
#
# Assumes PCF8574 at I2C address 0x20 (see i2c_addr)
#	Nothing connected to PCF8574 that will cause problems
#
#	This version requires Python V2.6+
#		Not Python 3
#
# PC Services PCF8574/Python-smbus example
# November 2012

# define I2C address of PCF8574 (0x20 to 0x27 are valid)
i2c_addr = 0x20

# import libraries
import smbus as smbus

#configure I2C bus for functions
i2c = smbus.SMBus(0)

# value to send
temp = 0x55

# Set PCF8574 outputs
try :
	print 'PCF8574 at address 0x{0:2x} WRITE 0x{1:2x}'.format( i2c_addr, temp )
	i2c.write_byte( i2c_addr, temp )
except IOError :
	print 'PCF8574 Device not found at I2C address 0x{1:2x}'.format( i2c_addr )
	error = 1
else :
	# Now read from PCF8574
	temp = i2c.read_byte( i2c_addr )
	print 'PCF8574 at address 0x{0:2x} READ 0x{1:2x}'.format( i2c_addr, temp )
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

PhilTheFish
Posts: 3
Joined: Tue Feb 12, 2013 1:16 pm

Re: Raspberry Pi and PICAXE using i2c

Tue Feb 12, 2013 8:18 pm

Thanks for that code.

I changed the i2c address to 0x50 and ran the code
it comes back with the following

PCF8574 at address 0x50 WRITE 0x12
PCF8574 at address 0x50 READ 0x0

It appears that it is not reading the value back.
I have tried various values but all come back as zero.

The PICAXE has got the following code in that just flashes an LED to let me know the program is scanning and puts up the debug window as well
I am also getting the values written to the scratchpad and putting them in b0,b1 and b2 for luck !!

init: hi2csetup i2cslave, %10100000
main:
high 0
pause 200
low 0
pause 200
get 0,b1
get 1,b2
get 2,b3
debug
goto main

No idea why?
Do you have any suggestions

Many Thanks
Phil

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Raspberry Pi and PICAXE using i2c

Tue Feb 12, 2013 8:58 pm

Not familiar with PICAXE code but the PCF8574 code works, I suggest you check the PICAXE I2C side of it.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

PhilTheFish
Posts: 3
Joined: Tue Feb 12, 2013 1:16 pm

Re: Raspberry Pi and PICAXE using i2c

Thu Feb 14, 2013 9:07 pm

Hi

I have had a play with the code you sent me and have had some success.
I can now read data that I have set in the PICAXE scratchpad with the following mod to the code that you sent

value=i2c.read_byte_Data(0x50,0x01)

This reads the value in scatchpad location 1 and returns the value I set in it.

I am still unable to write a value to the PICAXE
I am trying the following code

i2c.write_byte_data(0x50,0x00,0x38)
But when I try to read it back it comes back as zero and not 0x38

Also I am scanning for a hi2cflag in the PICAXE but it never changes to a 1 which indicates that the PICAXE has not recieved a write request.

Any suggestions
Thanks for your time.

Phil

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: Raspberry Pi and PICAXE using i2c

Thu Feb 14, 2013 9:56 pm

PhilTheFish wrote:Hi
I have had a play with the code you sent me and have had some success.
I can now read data that I have set in the PICAXE scratchpad with the following mod to the code that you sent

value=i2c.read_byte_Data(0x50,0x01)

This reads the value in scatchpad location 1 and returns the value I set in it.
Is '1' what you were expecting because PICAXE had written that value?
Does the PICAXE have addressable locations from I2C?
Is the PICAXE a single byte or multiple byte I2C device?
I am still unable to write a value to the PICAXE
I am trying the following code

i2c.write_byte_data(0x50,0x00,0x38)
But when I try to read it back it comes back as zero and not 0x38

Also I am scanning for a hi2cflag in the PICAXE but it never changes to a 1 which indicates that the PICAXE has not recieved a write request.

Any suggestions
Thanks for your time.

Phil
Hopefully someone with more experience of PICAXE can answer that.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

3n7r0py.pi
Posts: 1
Joined: Sat May 20, 2017 1:32 am

Re: Raspberry Pi and PICAXE using i2c

Fri Jun 02, 2017 3:13 am

I use info from this site http://learn.parallax.com/educators/tea ... asic-stamp to work on my piaxe. Was just getting ready to make a CO sensor for a raspberry pi. I'll share what I find as I get there.

http://www.parallax.com/go/PBASICHelp/C ... /I2CIN.htm
Page preview:

This code will transmit a "read" command to an I2C device (connected to I/O pins 0 and 1) and then will receive one byte and store it in the variable result. Though it may seem strange, the I2CIN command first transmits some data and then receives data. It must first transmit information (ID, read/write and address) in order to tell the I2C device what information it would like to receive. The exact information transmitted ($A1, 0) depends on the I2C device that is being used.

Code: Select all

result  VAR     Byte

Main:
  I2CIN 0, $A1, 0, [result]
  STOP

tempidoro
Posts: 8
Joined: Mon Apr 24, 2017 2:51 pm

Re: Raspberry Pi and PICAXE using i2c

Mon Jun 12, 2017 9:53 pm

I currently write data from Rpi to Picaxe 40X2. I had some problem trying to read the Picaxe scratchpad registers from Rpi. I solved this issue by reducing the baudrate in Rpi to 30KHz. I this a solution also to your troubleshoting?

User avatar
yv1hx
Posts: 372
Joined: Sat Jul 21, 2012 10:09 pm
Location: Zulia, Venezuela
Contact: Website

Re: Raspberry Pi and PICAXE using i2c

Mon Jun 12, 2017 11:31 pm

tempidoro wrote: ...snip ... the baudrate in Rpi to 30KHz. ...snip ...
baudrate to 30KHz? is not a typo?
Marco-Luis
Telecom Specialist (Now Available for Hire!)

http://www.meteoven.org
http://yv1hx.ddns.net
http://twitter.com/yv1hx

tempidoro
Posts: 8
Joined: Mon Apr 24, 2017 2:51 pm

Re: Raspberry Pi and PICAXE using i2c

Thu Jun 15, 2017 9:13 am

It was a mistake. The baudrate was 30k

Return to “Python”