4
\$\begingroup\$

I have an Atmega328P chip (being used as an Arduino) that I would like to connect to a Raspberry Pi via SPI.

I originally had an Atmega328P running on 3.3V using its own internal 8MHz crystal instead of an external 16MHz crystal. The Atmega was powered from the Raspberry Pi's 3.3v pin. I could directly connect MISO/MOSI/SCLK/CE0 pins between the two devices and I successfully used the Pi as SPI master and the Atmega as SPI slave.

Now I want to use an Atmega328 at 5V powered from external DC (LM317, etc) using an external 16MHz crystal. Obviously, the 5V Atmega pins can't talk to the 3.3V Pi pins so I got a Bi-Directional Logic Level Convertor (https://www.sparkfun.com/products/12009). This is my current schematic:

Schematic

My SPI code no longer works and I'm not quite sure why. Is this the correct way to hook up 5V Atmega and 3.3V Pi for SPI communication? Am I missing any connections?

UPDATE: I ended up trying out I2C communication and it worked right away, so I think I'm going to switch to that. I only needed low speed communication between the Atmega and Pi so it suits my needs. Someday I'll have to try out some of the suggestions below.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This question can only be resolved by accepting an answer. So either post in the answer form that you have switched to I2C as a solution, or accept the answer explaining the speed issues involved in using a translator intended for I2C for I2C instead, if you feel that is a more useful summary of the situation. \$\endgroup\$ Commented Oct 12, 2018 at 15:28

2 Answers 2

2
\$\begingroup\$

The problem with level converters like that is they can only go so fast. Try slowing down your SPI speed.

If you have a scope, check the levels on the signals to make sure they are high enough, especially the clock line, on both sides.

\$\endgroup\$
12
  • 1
    \$\begingroup\$ Level converters add a lot of capacitance on the bus. I agree with slowing it down, and checking levels on a scope. \$\endgroup\$
    – Annie
    Commented Oct 5, 2018 at 16:13
  • 1
    \$\begingroup\$ @Annie - it's not really the capacitance in this case. It's that these converters only drive high through pull-up resistors which by default are 10K. Adding external pull-up resistors of smaller value would increase the speed capability, or better going to a chip that can actively drive both polarities A bi-directional converter is not needed here, SPI lines are unidirectional in common usage. \$\endgroup\$ Commented Oct 5, 2018 at 16:19
  • \$\begingroup\$ You are both right. The added capacitance combined with weak pullups limits the speed. @ChrisStratton Good point on external pull-up resistors, that might work. \$\endgroup\$ Commented Oct 5, 2018 at 17:12
  • \$\begingroup\$ RC speed issues involve both resistance and capacitance of course. But while capacitance is increased it still remains in the picofarads. In contrast using one of these increases the resistance part of the problem in the rising-edge direction by many orders of magnitude compared to the chip's own drivers. The falling edge direction sees the capacitance of course, but it does not suffer nearly as much added resistance, and a check with a scope will show that the problem is the rising edges, not the falling ones. \$\endgroup\$ Commented Oct 5, 2018 at 17:17
  • 1
    \$\begingroup\$ Actually it's the other way around. They are primarily only hardware-based open drain when used in a special function mode such as I2C. If I recall, when used as GPIOs both chips mentioned in the question need to have open-drain behavior emulated in software, either making the pin an output low, or else an input. In contrast many more typical MCUs actually have an open-drain GPIO mode. \$\endgroup\$ Commented Oct 5, 2018 at 18:31
0
\$\begingroup\$

The capacitors C1 and C2 around your crystal are way too large and likely messing with your clock. Check the ATmega data sheet, they should be roughly 22pF.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.